千家信息网

Linux中grep和egrep命令如何使用

发表于:2025-12-03 作者:千家信息网编辑
千家信息网最后更新 2025年12月03日,这篇文章给大家介绍Linux中grep和egrep命令如何使用,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。rep / egrep语法: grep [-cinvABC] 'wor
千家信息网最后更新 2025年12月03日Linux中grep和egrep命令如何使用

这篇文章给大家介绍Linux中grep和egrep命令如何使用,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

rep / egrep

语法: grep [-cinvABC] 'word' filename

-c :打印符合要求的行数
-i :忽略大小写
-n :在输出符合要求的行的同时连同行号一起输出
-v :打印不符合要求的行
-A :后跟一个数字(有无空格都可以),例如 A2则表示打印符合要求的行以及下面两行
-B :后跟一个数字,例如 B2 则表示打印符合要求的行以及上面两行
-C :后跟一个数字,例如 C2 则表示打印符合要求的行以及上下各两行

把包含 'halt' 的行以及这行下面的两行都打印出。

[root@localhost ~]# grep -A2 'halt' /etc/passwdhalt:x:7:0:halt:/sbin:/sbin/haltmail:x:8:12:mail:/var/spool/mail:/sbin/nologinuucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin

把包含 'halt' 的行以及这行上面的两行都打印出。

[root@localhost ~]# grep -B2 'halt' /etc/passwdsync:x:5:0:sync:/sbin:/bin/syncshutdown:x:6:0:shutdown:/sbin:/sbin/shutdownhalt:x:7:0:halt:/sbin:/sbin/halt

把包含 'halt' 的行以及这行上面和下面的各两行都打印出。

过滤出带有某个关键词的行并输出行号

[root@localhost ~]# grep -n 'root' /etc/passwd1:root:x:0:0:root:/root:/bin/bash11:operator:x:11:0:operator:/root:/sbin/nologin

过滤不带有某个关键词的行,并输出行号

[root@localhost ~]# grep -nv 'nologin' /etc/passwd1:root:x:0:0:root:/root:/bin/bash6:sync:x:5:0:sync:/sbin:/bin/sync7:shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown8:halt:x:7:0:halt:/sbin:/sbin/halt26:test:x:511:511::/home/test:/bin/bash27:test1:x:512:511::/home/test1:/bin/bash

过滤出所有包含数字的行

[root@localhost ~]# grep '[0-9]' /etc/inittab# upstart works, see init(5), init(8), and initctl(8).# 0 - halt (Do NOT set initdefault to this)# 1 - Single user mode# 2 - Multiuser, without NFS (The same as 3, if you do not have networking)# 3 - Full multiuser mode# 4 - unused# 5 - X11# 6 - reboot (Do NOT set initdefault to this)id:3:initdefault:

过滤出所有不包含数字的行

[root@localhost ~]# grep -v '[0-9]' /etc/inittab# inittab is only used by upstart for the default runlevel.## ADDING OTHER CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.## System initialization is started by /etc/init/rcS.conf## Individual runlevels are started by /etc/init/rc.conf## Ctrl-Alt-Delete is handled by /etc/init/control-alt-delete.conf## Terminal gettys are handled by /etc/init/tty.conf and /etc/init/serial.conf,# with configuration in /etc/sysconfig/init.## For information on how to write upstart event handlers, or how## Default runlevel. The runlevels used are:#

把所有以 '#' 开头的行去除

[root@localhost ~]# grep -v '^#' /etc/inittabid:3:initdefault:

去除所有空行和以 '#' 开头的行

[root@localhost ~]# grep -v '^#' /etc/crontab |grep -v '^$'SHELL=/bin/bashPATH=/sbin:/bin:/usr/sbin:/usr/binMAILTO=rootHOME=/

在正则表达式中, "^" 表示行的开始, "$" 表示行的结尾,那么空行则可以用 "^$" 表示,如何打印出不以英文字母开头的行呢?

[root@localhost ~]# vim test.txt[root@localhost ~]# cat test.txt123abc456abc2323#laksdjfAlllllllll

先在test.txt中写几行字符串,用来做实验。

[root@localhost ~]# grep '^[^a-zA-Z]' test.txt123456#laksdjf
[root@localhost ~]# grep '[^a-zA-Z]' test.txt123456abc2323#laksdjf

如果是数字的话就用[0-9]这样的形式,当然有时候也可以用这样的形式[15]即只含有1或者5,注意,它不会认为是15。如果要过滤出数字以及大小写字母则要这样写[0-9a-zA-Z]。另外[ ]还有一种形式,就是[^字符] 表示除[ ]内的字符之外的字符。

过滤任意一个字符与重复字符

[root@localhost ~]# grep 'r..o' /etc/passwdoperator:x:11:0:operator:/root:/sbin/nologingopher:x:13:30:gopher:/var/gopher:/sbin/nologinvcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin

. 表示任意一个字符,上例中,就是把符合r与o之间有两个任意字符的行过滤出来, * 表示零个或多个前面的字符。

[root@localhost ~]# grep 'ooo*' /etc/passwdroot:x:0:0:root:/root:/bin/bashlp:x:4:7:lp:/var/spool/lpd:/sbin/nologinmail:x:8:12:mail:/var/spool/mail:/sbin/nologinuucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologinoperator:x:11:0:operator:/root:/sbin/nologinpostfix:x:89:89::/var/spool/postfix:/sbin/nologin

'ooo*' 表示oo, ooo, oooo ... 或者更多的 'o' 现在你是否想到了 '.*' 这个组合表示什么意义?

[root@localhost ~]# grep '.*' /etc/passwd |wc -l27[root@localhost ~]# wc -l /etc/passwd27 /etc/passwd

'.*' 表示零个或多个任意字符,空行也包含在内。

指定要过滤字符出现的次数

[root@localhost ~]# grep 'o\{2\}' /etc/passwdroot:x:0:0:root:/root:/bin/bashlp:x:4:7:lp:/var/spool/lpd:/sbin/nologinmail:x:8:12:mail:/var/spool/mail:/sbin/nologinuucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologinoperator:x:11:0:operator:/root:/sbin/nologinpostfix:x:89:89::/var/spool/postfix:/sbin/nologin

也可以不用脱意符\ 加上-E

grep -E 'o{2}' /etc/passwd

这里用到了{ },其内部为数字,表示前面的字符要重复的次数。上例中表示包含有两个o 即 'oo' 的行。注意,{ }左右都需要加上脱意字符 '\', 另外,使用{ }我们还可以表示一个范围的,具体格式是 '{n1,n2}' 其中n1

上面部分讲的grep,另外常常用到egrep这个工具,简单点讲,后者是前者的扩展版本,我们可以用egrep完成grep不能完成的工作,当然了grep能完成的egrep完全可以完成。如果你嫌麻烦,egrep了解一下即可,因为grep的功能已经足够可以胜任你的日常工作了。下面介绍egrep不用于grep的几个用法。为了试验方便,把test.txt 编辑成如下内容:

rot:x:0:0:/rot:/bin/bashoperator:x:11:0:operator:/root:/sbin/nologinoperator:x:11:0:operator:/rooot:/sbin/nologinroooot:x:0:0:/rooooot:/bin/bash1111111111111111111111111111111aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

筛选一个或一个以上前面的字符

[root@localhost ~]# egrep 'o+' test.txtrot:x:0:0:/rot:/bin/bashoperator:x:11:0:operator:/root:/sbin/nologinoperator:x:11:0:operator:/rooot:/sbin/nologinroooot:x:0:0:/rooooot:/bin/bash[root@localhost ~]# egrep 'oo+' test.txtoperator:x:11:0:operator:/root:/sbin/nologinoperator:x:11:0:operator:/rooot:/sbin/nologinroooot:x:0:0:/rooooot:/bin/bash[root@localhost ~]# egrep 'ooo+' test.txtoperator:x:11:0:operator:/rooot:/sbin/nologinroooot:x:0:0:/rooooot:/bin/bash

筛选零个或一个前面的字符

[root@localhost ~]# egrep 'o?' test.txtrot:x:0:0:/rot:/bin/bashoperator:x:11:0:operator:/root:/sbin/nologinoperator:x:11:0:operator:/rooot:/sbin/nologinroooot:x:0:0:/rooooot:/bin/bash1111111111111111111111111111111aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[root@localhost ~]# egrep 'ooo?' test.txtoperator:x:11:0:operator:/root:/sbin/nologinoperator:x:11:0:operator:/rooot:/sbin/nologinroooot:x:0:0:/rooooot:/bin/bash[root@localhost ~]# egrep 'oooo?' test.txtoperator:x:11:0:operator:/rooot:/sbin/nologinroooot:x:0:0:/rooooot:/bin/bash

筛选字符串1或者字符串2

[root@localhost ~]# egrep 'aaa|111|ooo' test.txtoperator:x:11:0:operator:/rooot:/sbin/nologinroooot:x:0:0:/rooooot:/bin/bash1111111111111111111111111111111aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

egrep中( )的应用

[root@localhost ~]# egrep 'r(oo)|(at)o' test.txtoperator:x:11:0:operator:/root:/sbin/nologinoperator:x:11:0:operator:/rooot:/sbin/nologinroooot:x:0:0:/rooooot:/bin/bash

用( )表示一个整体,例如(oo)+就表示1个 'oo' 或者多个 'oo'

[root@localhost ~]# egrep '(oo)+' test.txtoperator:x:11:0:operator:/root:/sbin/nologinoperator:x:11:0:operator:/rooot:/sbin/nologinroooot:x:0:0:/rooooot:/bin/bash

关于Linux中grep和egrep命令如何使用就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

字符 数字 输出 内容 后跟 多个 字符串 开头 形式 更多 空行 面的 行号 命令 两个 关键 关键词 大小 字母 就是 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 网络安全绘画 幼儿 反传销网络安全教育主题班会 从事网络安全专业方向有哪些岗位 生存服务器我的世界网易 mc史上最牛服务器 合肥app开发乐通网络技术不错 一个人能完成软件开发 互联网软件开发与维护 国家电网内网时间服务器地址 滨州数据库数据脱敏 软件开发人员如何写周报 人工神经网络技术上市公司 安次区网信办网络安全教育活动 深圳市第5届网络安全周 免疫组化蛋白数据库 软件开发如何进入银行 麻将软件开发常用指南 公安机关开展信息网络安全审核 5g时代网络安全面临的新挑战 psp无法连接服务器通讯 dnf服务器不同号邮件能收到吗 有关网络安全的看法 充电站网络安全检查内容 怎样将应用部署在服务器上 栈桥服务器 江苏电商软件开发要多少钱 如何做服务器raid plc的重量写入数据库 最低价网通服务器 如何配置阿里云服务器安全组
0