[root@localhost sed]# sed -n '/root/'p test.txt root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin [root@localhost sed]#
▶ -n 不打印无关的行 p 输出bash
▶若是不加 -n 则会把区配的关键字行打印两次,并列出无关的行ssh
[root@localhost sed]# sed '/root/'p test.txt root:x:0:0:root:/root:/bin/bash root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin
▶查找内容为ot的关键字行,可是须要脱义符 \ 例:post
[root@localhost sed]# sed -n '/o\+t/'p test.txt root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin [root@localhost sed]#
▶要不想使用脱义符 \ 须要在命令中 加 -r 选项code
[root@localhost 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@localhost sed]# sed -nr '/o{2}/'p test.txt ▶区配两次 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@localhost sed]#
▶区配 或者it
[root@localhost 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@localhost sed]#
[root@localhost sed]# sed -n '2'p test.txt ▶指定打印第二行 bin:x:1:1:bin:/bin:/sbin/nologin [root@localhost sed]#
▶能够指定范围test
[root@localhost 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@localhost sed]#
[root@localhost sed]# sed -n '15,$'p test.txt ▶打印15-末行 $表明末行 dbus:x:81:81:System message bus:/:/sbin/nologin polkitd:x:999:997:User for polkitd:/:/sbin/nologin postfix:x:89:89::/var/spool/postfix:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin chrony:x:998:996::/var/lib/chrony:/sbin/nologin [root@localhost sed]#
[root@localhost sed]# sed -e '1'p -e '/root/'p -n test.txt ▶打印第一行和区配 root 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@localhost sed]#
[root@localhost sed]# sed -n '/bus/'Ip test.txt ▶加上 I 能够将大小写的关键字一同区配出来 games:x:12:100:games:/usr/games:/sbin/nologinbus nobody:x:99:99:Nobody:/:/sbin/nologinBUS dbus:x:81:81:System message bus:/:/sbin/nologin [root@localhost sed]#
[root@localhost sed]# sed '1,15'd test.txt ▶只是在打印时把匹配以后剩下的打印出来,不实际删除 polkitd:x:999:997:User for polkitd:/:/sbin/nologin postfix:x:89:89::/var/spool/postfix:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin chrony:x:998:996::/var/lib/chrony:/sbin/nologin [root@localhost sed]# wc -l test.txt 19 test.txt [root@localhost sed]#
[root@localhost sed]# sed -i '1,15'd test.txt [root@localhost sed]# [root@localhost sed]# wc -l test.txt 4 test.txt [root@localhost sed]#
[root@localhost sed]# sed -i '/root/'d test.txt ▶也能够指定关键字相关行删除 [root@localhost sed]#
[root@localhost sed]# sed '1,10s'/root/toor/g test.txt ▶s 替换 ,g 全局替换 toor:x:0:0:toor:/toor:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin ....会所有打印出来
[root@localhost sed]# head test.txt |sed 's/\/nologin/123/g'▶将文件前10行中的nollogin替换成123 root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin123 daemon:x:2:2:daemon:/sbin:/sbin123 adm:x:3:4:adm:/var/adm:/sbin123 lp:x:4:7:lp:/var/spool/lpd:/sbin123 ▶注意:/ 之间必定要用 \ 隔开,否则会报错,或者用@ @, # # 隔开;
[root@localhost 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@localhost sed]#
[root@localhost sed]# head test.txt |sed -r 's/(.*)/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 [root@localhost sed]#