在每行行首添加一串字符串
sed 's/^/字符串/' 文件名
[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.110
Bcast:192.168.0.255
Linux环境下在文件的行首或者行尾添加字符串方法(干货)
1. sed 's/$/ aischang/' abc.txt >> test.txt
将文件abc.txt追加 aischang到每行末尾,写入到文件test.txt, $为末尾符号,$代表替换末尾为 aischang
2. sed 's/^/ aischang/' abc.txt >> test.txt
将文件abc.txt追加 aischang到每行首,写入到文件test.txt, ^为行首符号,^代表替换为 aischang
如果我们用一变量var,它的值根据上下文变化
$ var=”comm”,定义了变量,那么我们在sed的使用中这样使用变量
$ sed -n ‘/’”$var”‘/p’ /tmp/test.log
注意,是用单引号包含双引号来引用变量。
最初接触这个命令的人,可能以为$var之前的单引号会结束当前的script,其实不会。
后向引用
[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'
456QWE123
z注释: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
[root@www ~]# echo -n "xiaofeng666" |md5sum |cut -d' ' -f1
03083f95855f9b5a1ffa35eeecf6dc22
[root@www ~]# echo "xiaofeng666"|md5sum |cut -d ' ' -f1
56eef411bfbf7495f9265eb5da71fa1e
[root@www ~]#
[root@www linshi]# md5sum fstab.sh >fstab.sh.md5
[root@www linshi]# vim fstab.sh
[root@www linshi]# md5sum -c fstab.sh.md5
fstab.sh: FAILED
md5sum: WARNING: 1 of 1 computed checksum did NOT match
[root@www linshi]# cat fstab.sh.md5
88a069a93872f62fcd7aa03b8d78ae93 fstab.sh
[root@www linshi]# vim fstab.sh.md5
[root@www linshi]# vim fstab.sh
[root@www linshi]# md5sum -c fstab.sh.md5
fstab.sh: FAILED
md5sum: WARNING: 1 of 1 computed checksum did NOT match
[root@www linshi]# vim fstab.sh
[root@www linshi]# md5sum -c fstab.sh.md5
fstab.sh: OK
[root@www linshi]#
注释:输出一个系统随机数,计算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'
1
2
3
4
5
6
7
8
9
注释:输出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% /dev
tmpfs tmpfs 2.1G 0 2.1G 0% /dev/shm
tmpfs tmpfs 2.1G 8.7M 2.0G 1% /run
tmpfs 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% /var
tmpfs tmpfs 410M 0 410M 0% /run/user/0
[root@b ~]# cat b.txt |sed '/tmpfs/!d'
devtmpfs devtmpfs 2.0G 0 2.0G 0% /dev
tmpfs tmpfs 2.1G 0 2.1G 0% /dev/shm
tmpfs tmpfs 2.1G 8.7M 2.0G 1% /run
tmpfs tmpfs 2.1G 0 2.1G 0% /sys/fs/cgroup
tmpfs 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语言的所有注释
删除含有//的注释行
删除/*……*/的注释行
删除/* 至*/之间的所有内容
过滤出空格或者tab键开头的,和空行的行,打印出来并显示行号
[root@localhost linshi]# sed -nr '/^[\t ]+|^$/=' passwd