[root@b ~]# cat f.txt
inet addr:192.168.0.110 Bcast:192.168.0.255 Mask:255.255.255.0[root@b ~]# cat f.txt | sed -nr 's/^.*inet addr:(.*) Bcast:.*$/ip:\1/gp'ip:192.168.0.110[root@b ~]# cat f.txt | sed -nr 's/^.*inet addr:(.*) Bcast:(.*) Mask:.*$/ip:\1\nBcast:\2/gp'ip:192.168.0.110Bcast:192.168.0.255
后向引用
[root@b ~]# cat m.txt
123qwe456
[root@b ~]# cat m.txt |sed -r 's/([0-9]{3})([a-z]{3})([0-9]{3})/\3\2\1/g'|tr 'a-z' 'A-Z'
456QWE123z注释:s/([0-9]{3})([a-z]{3})([0-9]{3})/\3\2\1/g,表示匹配的123 qwe 456
tr 'a-z' 'A-Z'表示将小写字母a-z替换为大写字母A-Z
[root@b ~]# cat m.txt |sed -r 's/([0-9]{3})([a-z]{3})([0-9]{3})/\3\U\2\1/g'
456QWE123注释:\U表示将其后面的所有小写内容替换为大写字母
[root@b ~]# cat m.txt |sed -r 's/([0-9]{3})([a-z]{3})([0-9]{3})/\3\U\2\1/g'|sed -r 's/(.*)([A-Z]{3})(.*)/\3\L\2\1/g'
123qwe456注释:\L表示将其后面的所有大写内容替换为小写字母
[root@b ~]# echo $RANDOM | md5sum |tr '0-9' 'a-z' |cut -c 1-8
fcdfcdfh注释:输出一个系统随机数,计算md5的hash值,然后替换所有数字为小写字母,通过cut命令提取前8位数字
[root@b ~]# for i in `seq 1000`;do echo $RANDOM |md5sum|tr '0-9' 'a-z' | cut -c 1-10 >>l.txt;done
注释:使用for循环1000次,执行random系统随机数,并将结果输出到l.txt文件中
[root@b ~]# cat l.txt |sort |uniq -c | sort -nr |awk '$1>1{print $0}'|wc -l
注释:查看l.txt文件,通过sort排序和uniq去重操作后,使用awk命令将重复次数大于1的行打印出来,通过wc -l进行行数统计
[root@b ~]# echo '123456789' | sed -r 's/./&\n/g'|sed '$d'
123456789注释:输出123456789到管道,通过sed命令过滤,s/./&\n/g表示将任意一个字符替换为在其后面加\n换行符,再通过sed ‘$d’ 命令去除最后一行空行
[root@b ~]# cat b.txt
Filesystem Type Size Used Avail Use% Mounted on/dev/mapper/cl_b-root xfs 36G 12G 25G 32% /devtmpfs devtmpfs 2.0G 0 2.0G 0% /devtmpfs tmpfs 2.1G 0 2.1G 0% /dev/shmtmpfs tmpfs 2.1G 8.7M 2.0G 1% /runtmpfs tmpfs 2.1G 0 2.1G 0% /sys/fs/cgroup/dev/sda1 xfs 297M 122M 176M 41% /boot/dev/mapper/cl_b-home xfs 2.0G 33M 2.0G 2% /home/dev/mapper/cl_b-var xfs 4.3G 389M 4.0G 9% /vartmpfs tmpfs 410M 0 410M 0% /run/user/0[root@b ~]# cat b.txt |sed '/tmpfs/!d'devtmpfs devtmpfs 2.0G 0 2.0G 0% /devtmpfs tmpfs 2.1G 0 2.1G 0% /dev/shmtmpfs tmpfs 2.1G 8.7M 2.0G 1% /runtmpfs tmpfs 2.1G 0 2.1G 0% /sys/fs/cgrouptmpfs tmpfs 410M 0 410M 0% /run/user/0注释:sed '/tmpfs/!d'表示匹配tmpfs的行保留,其余内容全部删除。!表示取反的意思
[root@b ~]# cat -E m.txt
123qwe456$ $$$$ $[root@b ~]# cat m.txt | sed -r '/^$/d' #只删除了空行,无法删除空格行
123qwe456
[root@b ~]# cat m.txt | sed -r '/^$|[ \t]+/d' #同时删除空行和空格行
123qwe456[root@b ~]#
[root@b ~]# cat -n a.txt
1 flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 2 inet 192.168.0.104 netmask 255.255.255.0 broadcast 192.168.0.255 3 inet6 fe80::ad90:d426:9937:f9cd prefixlen 64 scopeid 0x20<link> 4 ether 00:0c:29:60:68:5a txqueuelen 1000 (Ethernet) 5 RX packets 1948 bytes 207628 (202.7 KiB) 6 RX errors 0 dropped 0 overruns 0 frame 0 7 TX packets 1203 bytes 194327 (189.7 KiB) 8 TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 9[root@b ~]# cat -n a.txt | sed '5,7s/RX/riririri/g'
1 flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 2 inet 192.168.0.104 netmask 255.255.255.0 broadcast 192.168.0.255 3 inet6 fe80::ad90:d426:9937:f9cd prefixlen 64 scopeid 0x20<link> 4 ether 00:0c:29:60:68:5a txqueuelen 1000 (Ethernet) 5 riririri packets 1948 bytes 207628 (202.7 KiB) 6 riririri errors 0 dropped 0 overruns 0 frame 0 7 TX packets 1203 bytes 194327 (189.7 KiB) 8 TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 9注释:sed '5,7s/RX/riririri/g'表示将5-7行的所有RX替换为riririri字符串。
[root@b ~]# cat -n a.txt | sed -n '1,3p' #打印1-3行,共三行
1 flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 2 inet 192.168.0.104 netmask 255.255.255.0 broadcast 192.168.0.255 3 inet6 fe80::ad90:d426:9937:f9cd prefixlen 64 scopeid 0x20<link>[root@b ~]# cat -n a.txt | sed -n '1p;3p' #打印1行和3行,共两行
1 flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 3 inet6 fe80::ad90:d426:9937:f9cd prefixlen 64 scopeid 0x20<link>[root@b ~]# cat -n a.txt | sed -n '1,+3p' #打印1行和其后的3行,共四行
1 flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 2 inet 192.168.0.104 netmask 255.255.255.0 broadcast 192.168.0.255 3 inet6 fe80::ad90:d426:9937:f9cd prefixlen 64 scopeid 0x20<link> 4 ether 00:0c:29:60:68:5a txqueuelen 1000 (Ethernet)
sed的多条件过滤语句写法
[root@b ~]# cat f.txt
inet addr:192.168.0.110 Bcast:192.168.0.255 Mask:255.255.255.0[root@b ~]# cat f.txt |sed -e 's/inet/forward/' -e 's/addr/home/' forward home:192.168.0.110 Bcast:192.168.0.255 Mask:255.255.255.0[root@b ~]# cat f.txt |sed 's/inet/forward/;s/addr/home/'
forward home:192.168.0.110 Bcast:192.168.0.255 Mask:255.255.255.0[root@b ~]# cat f.txt |sed -e '{
s/inet/forward/s/addr/home/}' forward home:192.168.0.110 Bcast:192.168.0.255 Mask:255.255.255.0
[root@b ~]# cat f.txt
inet addr:192.168.0.110 Bcast:192.168.0.255 Mask:255.255.255.0[root@b ~]# cat s.txt
s/inet/forward/s/addr/home/[root@b ~]# sed -f s.txt f.txt forward home:192.168.0.110 Bcast:192.168.0.255 Mask:255.255.255.0[root@b ~]# sed -r -f s.txt f.txt
forward home:192.168.0.110 Bcast:192.168.0.255 Mask:255.255.255.0注释:sed 的-f参数,表示过滤的条件都来自于s.txt文件
删除c语言的所有注释
删除含有//的注释行
删除/*……*/的注释行
删除/* 至*/之间的所有内容