grep实现的只是查找功能,而不能把查找的内容替换。以前vim操作文档的时候,可以查找也可以替换,但只限于在文本内部操作,而不能输出到屏幕上。sed工具以及后面要介绍的awk工具就能把替换的文本输出到屏幕上。
sed打印某行
sed命令的格式为:sed -n ‘n‘p filename,单引号内的n是一个数字,表示第几行。-n选项的作用是只显示要打印的行
[root@lizhipeng01 ~]# mkdir sed
[root@lizhipeng01 ~]# cd sed
[root@lizhipeng01 sed]# cp ../grep/passwd test.txt
[root@lizhipeng01 sed]# sed -n ‘/root/‘p test.txt
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@lizhipeng01 sed]# sed -nr ‘/r+t/‘p test.txt 加上n不用脱意
[root@lizhipeng01 sed]# sed -nr ‘/o+t/‘p test.txt
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@lizhipeng01 sed]# sed -nr ‘/o{2}/‘p test.txt 匹配2次o
root:x:0:0:root:/root:/bin/bash
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
[root@lizhipeng01 sed]# sed -nr ‘/root|bus/‘p test.txt 匹配root或者bus
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
[root@lizhipeng01 sed]# sed -n ‘2‘p test.txt 打印第2行
bin:x:1:1:bin:/bin:/sbin/nologin
[root@lizhipeng01 sed]# sed -n ‘2,5‘p test.txt 2到5行
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
[root@lizhipeng01 sed]# sed -n ‘1,$‘p test.txt 从头到尾
sed命令加上-e选项可以实现多个行为
[root@lizhipeng01 sed]# sed -e ‘1‘p -e ‘/bus/‘p -n test.txt 先打印第一行,再打印匹配bus的行
root:x:0:0:root:/root:/bin/bash
dbus:x:81:81:System message bus:/:/sbin/nologin
[root@lizhipeng01 sed]# sed -e ‘1‘p -e ‘/root/‘p -n test.txt
root:x:0:0:root:/root:/bin/bash
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@lizhipeng01 sed]# sed -n ‘/bus/‘Ip test.txt I忽略大小写,grep -i
222222222.r.olo:ddedBUs
dbus:x:81:81:System message bus:/:/sbin/nologin
删除某些行
[root@lizhipeng01 sed]# sed ‘1,20‘d test.txt 删除1到20行,并没有对test.txt文件做修改,只是不显示1到20行
lizhipeng:x:1000:1000::/home/lizhipeng:/bin/bash
user1:x:1001:1001::/home/user1:/bin/bash
lzp:x:1002:1003::/home/lzp:/bin/bash
user2:x:1003:1006::/home/user2:/bin/bash
user3:x:1004:1005::/home/user3:/bin/bash
user4:x:1006:1005::/home/lizhipeng001:/sbin/nologin
user5:x:1007:1007::/home/user5:/bin/bash
[root@lizhipeng01 sed]# wc -l test.txt
27 test.txt
[root@lizhipeng01 sed]# cp test.txt test.txt.bak 备份
[root@lizhipeng01 sed]# sed -i ‘1,20‘d test.txt -i ,删除第1到20行,对文件本身有影响
[root@lizhipeng01 sed]# wc -l test.txt
7 test.txt
[root@lizhipeng01 sed]# sed -i ‘/user2/‘d test.txt 删除user2
[root@lizhipeng01 sed]# cat test.txt
lizhipeng:x:1000:1000::/home/lizhipeng:/bin/bash
user1:x:1001:1001::/home/user1:/bin/bash
lzp:x:1002:1003::/home/lzp:/bin/bash
user3:x:1004:1005::/home/user3:/bin/bash
user4:x:1006:1005::/home/lizhipeng001:/sbin/nologin
user5:x:1007:1007::/home/user5:/bin/bash
[root@lizhipeng01 sed]# cp test.txt.bak test.txt
cp:是否覆盖"test.txt"? y
[root@lizhipeng01 sed]# sed ‘1,10s/root/toor/g‘ test.txt 查找替换,1到10行,root替换为toor,对文件本身没有影响
toor:x:0:0:toor:/toor:/bin/bash
[root@lizhipeng01 sed]# sed -r ‘1,10s/ro+/r/g‘ test.txt |head
rt:x:0:0:rt:/rt:/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
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/rt:/sbin/nologin
[root@lizhipeng01 sed]# head test.txt |sed -r ‘s/([^:]+):(.*):([^:]+)/\3:\2:\1/‘
/bin/bash:x:0:0:root:/root:root
/sbin/nologin:x:1:1:bin:/bin:bin
/sbin/nologin:x:2:2:daemon:/sbin:daemon
/sbin/nologin:x:3:4:adm:/var/adm:adm
/sbin/nologin:x:4:7:lp:/var/spool/lpd:lp
/bin/sync:x:5:0:sync:/sbin:sync
/sbin/shutdown:x:6:0:shutdown:/sbin:shutdown
/sbin/halt:x:7:0:halt:/sbin:halt
/sbin/nologin:x:8:12:mail:/var/spool/mail:mail
/sbin/nologin:x:11:0:operator:/root:operator
[root@lizhipeng01 sed]# head test.txt |sed ‘s/[a-zA-Z]//g‘
::0:0::/://
::1:1::/://
::2:2::/://
::3:4:://://
::4:7::///://
::5:0::/://
::6:0::/://
::7:0::/://
::8:12::///://
::11:0::/://
[root@lizhipeng01 sed]# head test.txt |sed -r ‘s/(.*)/aaa:&/‘ 在所有行之前添加aaa:
aaa:root:x:0:0:root:/root:/bin/bash
aaa:bin:x:1:1:bin:/bin:/sbin/nologin
aaa:daemon:x:2:2:daemon:/sbin:/sbin/nologin
aaa:adm:x:3:4:adm:/var/adm:/sbin/nologin
aaa:lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
aaa:sync:x:5:0:sync:/sbin:/bin/sync
aaa:shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
aaa:halt:x:7:0:halt:/sbin:/sbin/halt
aaa:mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
aaa:operator:x:11:0:operator:/root:/sbin/nologin