Linux文本处理三剑客之grep

grep: Global search REgular expression and Print out the line

作用:文本搜索工具,根据用户指定的“模式”对目标文本逐行进行匹配检查;打印匹配到的行

模式:由正则表达式字符及文本字符所编写的过滤条件

格式

grep [OPTIONS] PATTERN [FILE...]

常见选项

--color=auto    对匹配到的文本加上颜色显示
-m #            匹配#次后停止
-v              显示不被pattern匹配到的行
-i              忽略字符大小写
-n              显示匹配的行号
-c              统计匹配的行数
-o              仅显示匹配到的字符串
-q              静默模式,不输出任何信息
-A # after      后#行
-B # before     前#行
-C # context    前后个#行
-e              实现多个选项间的逻辑or关系,如:grep -e 'cat' -e 'dog' file
-w              匹配整个单词
-E              使用ERE,相当于egrep
-F              相当于使用fgrep,不支持正则表达式
-f file         根据模式文件处理
-r              递归目录,但不处理软连接
-R              递归目录,但处理软连接

范例

[19:47:02 root@centos8 ~]#grep root /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[20:50:11 root@centos8 ~]#grep "USER" /etc/passwd
[20:50:23 root@centos8 ~]#grep 'USER' /etc/passwd
[20:50:38 root@centos8 ~]#grep whoami /etc/passwd
[20:50:53 root@centos8 ~]#grep `whoami` /etc/passwd
root:x:0:0:root:/root:/bin/bash

范例

#找出最高的磁盘利用率
[20:58:29 root@centos8 ~]#df |grep "^/dev" |tr -s ' ' %|cut -d% -f5 |sort -nr|head -1

#统计访问本机ip链接数最多的3个ip
[21:03:11 root@centos8 ~]#ss -nt |grep ESTAB |tr -s " " :|cut -d: -f6|sort |uniq -c|sort -nr |head -3

#不匹配以#号开头和空行的行
[21:09:18 root@centos8 ~]#grep -v "^#" /etc/profile |grep -v "^$"
[21:09:18 root@centos8 ~]#grep -v "^#\|^$" /etc/profile
[21:09:18 root@centos8 ~]#grep -v "^\(#\|$\)" /etc/profile
[21:09:18 root@centos8 ~]#grep -Ev "^(#|$)" /etc/profile
[21:09:18 root@centos8 ~]egrep -v "^(#|$)" /etc/profile
[21:09:18root@centos8~]#egrep'^(#|$)'/etc/httpd/conf/httpd.conf|wc -l

范例

[21:09:18 root@centos8 ~]#grep -o "r..t" /etc/passwd
root
root
root
root
r/ft
#匹配ip地址
[21:15:22 root@centos8 ~]#ip a| grep -E '[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}'
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    link/ether 00:0c:29:23:78:0c brd ff:ff:ff:ff:ff:ff
    inet 10.0.0.101/24 brd 10.0.0.255 scope global dynamic noprefixroute ens160
    inet6 fe80::91a5:9a9f:1c42:10a/64 scope link noprefixroute 

[21:15:59 root@centos8 ~]#ip a|grep -E '([0-9]{1,3}.){3}[0-9]{1,3}'
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    link/ether 00:0c:29:23:78:0c brd ff:ff:ff:ff:ff:ff
    inet 10.0.0.101/24 brd 10.0.0.255 scope global dynamic noprefixroute ens160
    inet6 fe80::91a5:9a9f:1c42:10a/64 scope link noprefixroute 

[21:20:40 root@centos8 ~]#ip a|grep -Eo '([0-9]{1,3}\.){3}[0-9]{1,3}'|head -1
127.0.0.1

[21:25:22 root@centos8 ~]#cat regex.txt 
([0-9]{1,3}\.){3}[0-9]{1,3}
[21:25:20 root@centos8 ~]#ip a |grep -Eof regex.txt 
127.0.0.1
10.0.0.101
10.0.0.255

范例

[21:25:38 root@centos8 ~]#grep -E 'root|bash' /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
ding:x:1000:1000:ding:/home/ding:/bin/bash
wang:x:1001:1001::/home/wang:/bin/bash
nginx:x:1004:1005::/home/nginx:/bin/bash
varnish:x:1005:1006::/home/varnish:/bin/bash
tomcat:x:1007:1008::/home/tomcat/:/bin/bash
[21:27:18 root@centos8 ~]#grep -e 'root' -e 'bash' /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
ding:x:1000:1000:ding:/home/ding:/bin/bash
wang:x:1001:1001::/home/wang:/bin/bash
nginx:x:1004:1005::/home/nginx:/bin/bash
varnish:x:1005:1006::/home/varnish:/bin/bash
tomcat:x:1007:1008::/home/tomcat/:/bin/bash

[21:28:12 root@centos8 ~]#grep -w root /etc/passwd
root:x:0:0:root:/root:/bin/bash
[21:28:34 root@centos8 ~]#grep '\' /etc/passwd
root:x:0:0:root:/root:/bin/bash
#匹配词首和此为相同的行
[21:32:03 root@centos8 ~]#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
[21:42:47 root@centos8 ~]#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
[21:44:34 root@centos8 ~]#egrep "^(.*)\>.*\<\1$" /etc/passwd
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:hlt:/sbin:/sbin/halt

你可能感兴趣的:(Linux文本处理三剑客之grep)