- grep 命令主要对文本的(正则表达式)行基于模式进行过滤
- sed stream editor,文本编辑工具
- awk Linux上的实现gawk,文本报告生成器
grep:Global search REgular expression and Print out the line
作用:文本搜索工具,根据用户指定的“模式”对目标文本逐行进行匹配检查,打印匹配到的行;
模式:由正则表达式字符及文本字符所编写的过滤条件;
#帮助
https://man7.org/linux/man-pages/man1/grep.1.html
#格式
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
-P 支持Perl格式的正则表达式
-f file 根据模式文件处理
-r 递归目录,但不处理软链接
-R 递归目录,但处理软链接
#范例
#取两个文件相同行
[root@centos79_test test]# cat g1.txt
a
b
s
f
hr
r
sd
fg
sdf
e
a
a
d
q
[root@centos79_test test]# cat g2.txt
gh
sd
h
x
b
h
f
s
hr
[root@centos79_test test]# grep -f g1.txt g2.txt
sd
b
f
s
hr
[root@centos79_test test]# cat g1.txt g2.txt | sort | uniq -d
#范例
#分区利用率最大的值
[root@centos79_test test]# df -h | grep '^/dev/sd' | tr -s ' ' % | cut -d% -f5 | sort -n | tail -1
15
[root@centos79_test test]# df |grep '^/dev/sd' |grep -oE '\<[0-9]{,3}%'|tr -d '%'|sort -
nr|head -n1
15
[root@centos79_test test]# df |grep '^/dev/sd' |grep -oE '\<[0-9]{,3}%'|grep -Eo '[0-9]+'
|sort -nr|head -n1
15
#范例
#哪个IP和当前主机连接数最多
[root@centos79_test test]# ss -nt | grep "^ESTAB" |tr -s ' ' : |cut -d: -f6|sort |uniq -c|sort -nr|head -n1
2 192.168.1.1
#连接状态的统计
[root@centos79_test test]# ss -nta | grep -v '^State' |cut -d" " -f1|sort |uniq -c
2 ESTAB
6 LISTEN
[root@centos79_test test]# ss -nta | tail -n +2 |cut -d" " -f1|sort |uniq -c
2 ESTAB
6 LISTEN
#范例
#排除空行和#号注释行
grep -v "^#" /etc/fstab | grep -v '^$'
grep -v "^#\|^$" /etc/fstab
grep -v "^\(#\|$\)" /etc/fstab
grep -Ev "^(#|$)" /etc/fstab
egrep -v "^(#|$)" /etc/fstab
#范例
#查看IP地址
[root@centos79_test test]# ifconfig eth0 | grep --color=auto -Eo '([0-9]{1,3}\.){3}[0-9]{1,3}' | head -1
192.168.1.10
[root@centos79_test test]# ifconfig eth0 | grep --color=auto -Eo '([0-9]{1,3}\.){3}[0-9]{1,3}'
192.168.1.10
255.255.255.0
192.168.1.255
#范例
#算出所有人的年龄总和
[root@centos79_test test]# cat age.txt
xiaowang=22
xiaoli=23
xiaozhu=25
[root@centos79_test test]# cut -d"=" -f2 age.txt
22
23
25
[root@centos79_test test]# cut -d"=" -f2 age.txt | tr '\n' +
22+23+25+
[root@centos79_test test]# cut -d"=" -f2 age.txt | tr '\n' + | grep -Eo ".*[0-9]"
22+23+25
[root@centos79_test test]# cut -d"=" -f2 age.txt | tr '\n' + | grep -Eo ".*[0-9]" |bc
70
[root@centos79_test test]# grep -Eo "[0-9]" age.txt
2
2
2
3
2
5
[root@centos79_test test]# grep -Eo "[0-9]+" age.txt
22
23
25
[root@centos79_test test]# grep -Eo "[0-9]+" age.txt | tr "\n" +
22+23+25+
[root@centos79_test test]# grep -Eo "[0-9]+" age.txt | tr "\n" + | grep -Eo ".*[0-9]"
22+23+25
[root@centos79_test test]# grep -Eo "[0-9]+" age.txt | tr "\n" + | grep -Eo ".*[0-9]" | bc
70
[root@centos79_test test]# grep -Eo "[0-9]+" age.txt
22
23
25
[root@centos79_test test]# grep -Eo "[0-9]+" age.txt | paste -s -d+
22+23+25
[root@centos79_test test]# grep -Eo "[0-9]+" age.txt | paste -s -d+ | bc
70
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END