文本处理工具之grep
一、grep命令
作用:文本搜索工具,根据用户指定的“模式”对目标文本逐行进行匹配检查;打印匹配到的行
模式:由正则表达式字符及文本字符所编写的过滤条件
格式:
grep [OPTIONS] PATTERN [FILE...]
常见选项:
1--color=auto 对匹配到的文本着色显示
2 -m # 匹配#次后停止
3 -v 显示不被pattern匹配到的行,取反
4 -i 忽略字符大小写
5 -n 显示匹配的行号
6 -o 仅显示匹配到的字符串
7 -q 静默模式,不输出任何内容
8 -A # after,后#行
9 -B # before,前#行
10 -C # context,前后各#行
11 -e 实现多个选项间的逻辑or关系,如:grep -e 'cat' -e 'dog' file
12 -w 匹配整个单词
13 -E 使用ERE,相当于Egrep,扩展正则表达式
14 -F 不支持正则表达式,相当于fgrep
15 -f file 根据模式文件处理
16 -r 递归目录,不处理软链接
17 -R 递归目录,处理软链接
例
1 grep root /etc/passwd
2 grep "USER" /etc/passwd
3 grep 'USER' /etc/passwd
4 grep `whoami` /etc/passwd
例:取两个文件的相同行
-f :file 根据模式文件处理
[root@localhost tmp]# cat /tmp/f1.txt /tmp/f2.txt
a
b
1
c
b
e
f
c
2
[root@localhost tmp]# grep -f /tmp/f1.txt /tmp/f2.txt
b
e
f
c
1
2
例:取最大磁盘使用率
df:显示磁盘分区上可以使用的磁盘空间
df:显示磁盘分区上可以使用的磁盘空间
cut:命令 用于按列提取字符
sort:将文件的每一行作为一个单位,相互比较,比较原则是从首字符向后,依次按ASCII码值进行比较,最后将他们按升序输出
df | grep '^/dev/sd' |tr -s ' ' %|cut -d% -f5|sort -n|tail -1
例: 哪个IP和当前主机连接数最多的前三位
ss -nt | grep "^ESTAB" |tr -s ' ' : |cut -d: -f6|sort |uniq -c|sort -nr|head -n3
例:
-v: 显示不被pattern匹配到的行,取反
-E: 使用ERE,相当于Egrep,扩展正则表达式
[root@centos8 ~]#grep -v "^#" /etc/profile | grep -v '^$'
[root@centos8 ~]#grep -v "^#\|^$" /etc/profile
[root@centos8 ~]#grep -v "^\(#\|$\)" /etc/profile
[root@centos8 ~]#grep -Ev "^(#|$)" /etc/profile
[root@centos8 ~]#egrep -v "^(#|$)" /etc/profile
[root@centos6 ~]#egrep -v '^(#|$)' /etc/httpd/conf/httpd.conf
例:
-o: 仅显示匹配到的字符串
[root@localhost tmp]# grep -o 'r..t' /etc/passwd
root
root
root
root
r/ft
例:
-E: 使用ERE,相当于Egrep,扩展正则表达式
-o : 仅显示匹配到的字符串
-f :file 根据模式文件处理
[root@localhost tmp]# ifconfig | grep -E '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}'
inet 10.0.0.8 netmask 255.255.255.0 broadcast 10.0.0.255
[root@localhost tmp]# ifconfig | grep -E '([0-9]{1,3}\.){3}[0-9]{1,3}'
inet 10.0.0.8 netmask 255.255.255.0 broadcast 10.0.0.255
inet 127.0.0.1 netmask 255.0.0.0
inet 127.0.0.1 netmask 255.0.0.0
[root@localhost tmp]# ifconfig ens160 | grep -Eo '([0-9]{1,3}\.){3}[0-9]{1,3}'|head -1
10.0.0.8
[root@localhost tmp]# cat regex.txt
([0-9]{1,3}\.){3}[0-9]{1,3}
[root@localhost tmp]# ifconfig | grep -oEf regex.txt
10.0.0.8
255.255.255.0
10.0.0.255
127.0.0.1
255.0.0.0
例:
-E : 使用ERE,相当于Egrep,扩展正则表达式
-e : 实现多个选项间的逻辑or关系,如:grep -e ‘cat’ -e ‘dog’ file
[root@localhost tmp]# grep -E 'root|bash' /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@localhost tmp]# grep -e 'root' -e 'bash' /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
例:
-w : 匹配整个单词
grep -w root /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@localhost tmp]# grep '\<root\>' /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
例:查找出头跟尾相同
如果在不使用反义符号的情况下可以使用egrep或者grep -E ,egrep–>grep -E
-E : 使用ERE,相当于Egrep,扩展正则表达式
[root@localhost tmp]# grep "^\(.*\)\>.*\<\1$" /etc/passwd
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
[root@localhost tmp]# grep -E "^(.*)\>.*\<\1$" /etc/passwd
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
[root@localhost tmp]# egrep "^(.*)\>.*\<\1$" /etc/passwd
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt