10 文本处理三剑客
grep:文本过滤(模式:pattern)工具
- grep, egrep, fgrep(不支持正则表达式搜索)
sed:stream editor,文本编辑工具
awk:Linux上的实现gawk,文本报告生成器
10.1 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
- -F:相当于fgrep,不支持正则表达式,查找速度更快
- -f file: 根据模式文件处理(将正则表达式保存为文件后使用)
- -r 递归目录,但不处理软链接
- -R 递归目录,但处理软链接
范例:
[root@centos8 data]#grep root /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
roo:x:1000:1000:root:/home/roo:/bin/bash
[root@centos8 data]#grep '^root' /etc/passwd
root:x:0:0:root:/root:/bin/bash
[root@centos8 data]#grep 'bash$' /etc/passwd
root:x:0:0:root:/root:/bin/bash
roo:x:1000:1000:root:/home/roo:/bin/bash
cc1:x:1001:1001::/home/cc1:/bin/bash
cc2:x:1002:1003::/home/cc2:/bin/bash
范例:双引号、单引号、反引号的不同
[root@centos8 data]#grep "$USER" /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
roo:x:1000:1000:root:/home/roo:/bin/bash
[root@centos8 data]#grep '$USER' /etc/passwd
[root@centos8 data]#grep `whoami` /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
roo:x:1000:1000:root:/home/roo:/bin/bash
范例:查看分区信息;分区利用率;取利用率最大值
[root@centos8 data]#df | grep '^/dev/sd'
/dev/sda5 10475520 4888988 5586532 47% /
/dev/sda3 3135488 54924 3080564 2% /data
/dev/sda1 999320 203612 726896 22% /boot
[root@centos8 data]#df | grep '^/dev/sd'|tr -s ' ' %|cut -d% -f5
47
2
22
[root@centos8 data]#df | grep '^/dev/sd'|tr -s ' ' %|cut -d% -f5|sort -n |tail -1
47
范例:查询连接本机的远程主机IP地址排名前三
[root@centos8 data]#ss -nt|grep '^ESTAB'|tr -s ' ' :|cut -d: -f6|sort|uniq -c|sort -nr |head -3
2 10.0.0.100
2 10.0.0.1
范例: 显示/etc/profile文件, 使#行和空行不显示
#方法一:
[root@bogon ~]#grep -v "^#" /etc/profile | grep -v "^$"
pathmunge () {
case ":${PATH}:" in
*:"$1":*)
;;
*)
if [ "$2" = "after" ] ; then
PATH=$PATH:$1
else
PATH=$1:$PATH
fi
esac
}
if [ -x /usr/bin/id ]; then
...
#方法二:
[root@bogon ~]#grep -v "^#\|^$" /etc/profile
#方法三:
[root@bogon ~]#grep -v "^\(#\|$\)" /etc/profile
#方法四:扩展正则表达式
[root@bogon ~]#grep -Ev "^(#|$)" /etc/profile
#方法五:扩展正则表达式
[root@bogon ~]#egrep -v "^(#|$)" /etc/profile
范例:查找ifconfig中的IP地址
#方法一:
[root@bogon ~]#ifconfig | egrep '[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}' ##IP地址的正则表达式写法
inet 10.0.0.100 netmask 255.255.255.0 broadcast 10.0.0.255
inet6 fe80::6803:1b34:eb67:b279 prefixlen 64 scopeid 0x20<link>
inet 127.0.0.1 netmask 255.0.0.0
inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255
ether 52:54:00:03:6b:6b txqueuelen 1000 (Ethernet)
#方法二:
[root@bogon ~]#ifconfig | egrep '([0-9]{1,3}.){3}[0-9]{1,3}'
inet 10.0.0.100 netmask 255.255.255.0 broadcast 10.0.0.255
inet6 fe80::6803:1b34:eb67:b279 prefixlen 64 scopeid 0x20<link>
inet 127.0.0.1 netmask 255.0.0.0
inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255
ether 52:54:00:03:6b:6b txqueuelen 1000 (Ethernet)
#查找本机IP:
[root@bogon ~]#ifconfig | egrep -o '([0-9]{1,3}.){3}[0-9]{1,3}'|head -1
10.0.0.100
范例:查找root所在行的后3行
[root@bogon ~]#grep -nA3 root /etc/passwd
1:root:x:0:0:root:/root:/bin/bash
2-bin:x:1:1:bin:/bin:/sbin/nologin
3-daemon:x:2:2:daemon:/sbin:/sbin/nologin
4-adm:x:3:4:adm:/var/adm:/sbin/nologin
--
10:operator:x:11:0:operator:/root:/sbin/nologin
11-games:x:12:100:games:/usr/games:/sbin/nologin
12-ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
13-nobody:x:65534:65534:Kernel Overflow User:/:/sbin/nologin
范例:查找中"或"的不同方法
[root@bogon ~]#egrep 'root|bash' /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
cc1:x:1000:1000:cc1:/home/cc1:/bin/bash
cc2:x:1001:1001::/home/cc2:/bin/bash
[root@bogon ~]#egrep -e 'root' -e 'bash' /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
cc1:x:1000:1000:cc1:/home/cc1:/bin/bash
cc2:x:1001:1001::/home/cc2:/bin/bash
范例: -f用法
[root@bogon data]#echo '([0-9]{1,3}.){3}[0-9]{1,3}' > f1
[root@bogon data]#cat f1
([0-9]{1,3}.){3}[0-9]{1,3}
[root@bogon data]#ifconfig | egrep -f f1
inet 10.0.0.100 netmask 255.255.255.0 broadcast 10.0.0.255
inet6 fe80::6803:1b34:eb67:b279 prefixlen 64 scopeid 0x20<link>
RX packets 9692 bytes 2583901 (2.4 MiB)
inet 127.0.0.1 netmask 255.0.0.0
inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255
ether 52:54:00:03:6b:6b txqueuelen 1000 (Ethernet)
范例:哪个IP和当前主机连接数最多的前三位
[root@centos8 ~]#ss -nt | grep "^ESTAB" |tr -s ' ' : |cut -d: -f6|sort |uniq -c|sort -nr|head -n3
3 10.0.0.1
1 172.16.4.100
1 172.16.31.188
范例: 连接状态的统计
[root@wang-liyun-pc ~]# ss -nta | grep -v '^State' |cut -d" " -f1|sort |uniq -c
7 ESTAB
4 LISTEN
7 TIME-WAIT
[root@wang-liyun-pc ~]# ss -nta | tail -n +2 |cut -d" " -f1|sort |uniq -c
3 ESTAB
4 LISTEN
12 TIME-WAIT
[root@wang-liyun-pc ~]#
范例:算出所有人的年龄总和
[root@centos8 ~]#cat /data/age.txt
xiaoming=20
xiaohong=18
xiaoqiang=22
[root@centos8 ~]#cut -d"=" -f2 /data/age.txt|tr '\n' + | grep -Eo ".*[0-9]"|bc
60
[root@centos8 ~]#grep -Eo "[0-9]+" /data/age.txt | tr '\n' + | grep -Eo ".*[0-9]"|bc
60
[root@centos8 ~]#grep -oE '[0-9]+' /data/age.txt| paste -s -d+|bc
60
10.2 sed
10.2.1 sed 工作原理
sed 即 Stream EDitor,和 vi 不同,sed是行编辑器
Sed是从文件或管道中读取一行,处理一行,输出一行;再读取一行,再处理一行,再输出一行,直到最后一行。
每当处理一行时,把当前处理的行存储在临时缓冲区中,称为模式空间(Pattern Space),接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏幕。
接着处理下一行,这样不断重复,直到文件末尾。
一次处理一行的设计模式使得sed性能很高,sed在读取大文件时不会出现卡顿的现象。
如果使用vi命令打开几十M上百M的文件,明显会出现有卡顿的现象,这是因为vi命令打开文件是一次性将文件加载到内存,然后再打开。
Sed就避免了这种情况,一行一行的处理,打开速度非常快,执行速度也很快
10.2.2 sed基本用法
待续…