Sed
全称:stream editor
逐行处理读取文件
选项:
-i in-place 就地修改
-n silence 只打印编辑过的行(静默打印)
-e expression +脚本表达式 #只显示不修改
-f 执行脚本 +加脚本名
脚本:
-i insert 前插
-a append 在后追加
-d delete 删除
-c copy 覆盖
-s substitute 替换行中的某些字段
-p print 打印
-g global 全局替换
[root@ds1 ~]# vim test.txt
1
2
3
4
5
[root@ds1 ~]# sed -e '1i\hello sed' test.txt 第一行插入(1i\[要插入的内容])
hello sed #sed -e 只进行显示,不进行修改
1
2
3
4
5
[root@ds1 ~]# cat test.txt
1
2
3
4
5
#如何让修改生效?
[root@ds1 ~]# sed -ie '1i\hello sed' test.txt # sed -i进行修改,sed会对原文件备份
[root@ds1 ~]# cat test.txt
hello sed
1
2
3
4
5
# sed -i进行修改,在i后加入字母,sed会对原文件备份
[root@ds1 ~]# ll
-rw-r--r--. 1 root root 21 4月 22 22:51 test.txt
-rw-r--r--. 1 root root 11 4月 22 22:47 test.txte
[root@ds1 ~]# cat test.txte
1
2
3
4
5
[root@ds1 ~]# sed -i '1i\new new old' test.txt
[root@ds1 ~]# sed -e '' test.txt
new new old
hello sed
1
2
3
4
5
[root@ds1 ~]# sed -i '1s/old/new/' test.txt
[root@ds1 ~]# sed -n '1p' test.txt
new new new
整体替换和局部替换
局部:/old/new/ #某一行的全局替换//g
全局:/old/new/g
举一个例子
cat sed.txt
1111111
22222222
11
sed -i 's/1/6/' sed.txt
cat sed.txt
6111111
22222222
61
可以看到上述示例只会把每一行第一个符合的字符替换,而不会替换每行中所有符合项
全局替换
sed -i 's/1/6/g' sed.txt
cat sed.txt
6666666
22222222
66
匹配到具有相同字段的不同行,只替换其中一行的内容该怎么做?
[root@lyh ~]# vim passwd.txt
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
nscd:x:28:28:NSCD Daemon:/:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
nginx:x:997:993:Nginx web server:/var/lib/nginx:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/bash
过滤含"daemon"的行
[root@lyh ~]# sed -n '/daemon/p' passwd.txt
daemon:x:2:2:daemon:/sbin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/bash
只把匹配到的第二行中的daemon改为haha该怎么处理
[root@lyh ~]# sed -n '/bash$/s/daemon/haha/gp' passwd.txt
haha:x:2:2:haha:/sbin:/sbin/bash
[root@lyh ~]# sed -i '/bash$/s/daemon/haha/g' passwd.txt
[root@lyh ~]# tail -n 1 passwd.txt
haha:x:2:2:haha:/sbin:/sbin/bash
打印第三行和第六行内容
[root@lyh ~]# sed -n '3p;6p' passwd.txt
daemon:x:2:2:daemon:/sbin:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
打印3到6行内容
[root@lyh ~]# sed -n '3,6p' passwd.txt
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
查找10.192.203.181在第几行
[root@lyh ~]# sed -n '/10.192.203.181/=' IP.txt
7
过滤空行
vim test.txt
10.192.120.25
10.192.120.27
10.192.120.36
10.192.120.37
10.192.120.55
10.192.120.56
sed -i '/^$/d' test.txt
1-3行添加注释
sed -i '1,3s/^/#/' test.txt
去掉1-3行的注释
sed -i '1,3s/^#//' test.txt
head和tail命令截取文本
截取2-5行内容
[root@lyh ~]# cat IP.txt
10.192.120.25
10.192.120.27
10.192.120.36
10.192.120.37
10.192.120.55
10.192.120.56
10.192.203.181
10.192.203.182
10.192.203.183
10.192.203.184
10.192.203.185
10.192.203.186
10.192.203.187
[root@lyh ~]# cat -n IP.txt| head -5 |tail -4
2 10.192.120.27
3 10.192.120.36
4 10.192.120.37
5 10.192.120.55
#查看内容在哪几行出现
[root@lyh ~]# sed -n '/186/=' IP.txt
12