2018-1-16 6周2次课 sed
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2018-1-16 6周2次课 sed相关的知识,希望对你有一定的参考价值。
9.4/9.5 sed
sed的优势在于替换指定的字符
sed如何实现grep的功能
sed -n (--quite --silent)
-r 脱义
p print 打印
·匹配指定的行:
[[email protected] sed]# sed -n '/root/'p test.txt ##匹配root root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin [[email protected] sed]# sed -n '/r.t/'p test.txt operator:x:11:0:operator:/root:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin [[email protected] sed]# sed -n '/r*t/'p test.txt root:x:0:0:root:/root:/bin/bash shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt operator:x:11:0:operator:/root:/sbin/nologin ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin dbus:x:81:81:System message bus:/:/sbin/nologin polkitd:x:999:997:User for polkitd:/:/sbin/nologin postfix:x:89:89::/var/spooooool/postfix:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin [[email protected] sed]# sed -nr '/o+t/'p test.txt ## -r 脱义,o有1次或多次 root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin [[email protected] sed]# sed -n '/o\+t/'p test.txt ## o有1次或多次 root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin [[email protected] 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/spooooool/postfix:/sbin/nologin chrony:x:998:996::/var/lib/chroooony:/sbin/nologin [[email protected] sed]# sed -nr '/root|bus/'p test.txt ##匹配 | 或者 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
·打印指定的行:
[[email protected] sed]# sed -n '2'p test.txt ##打印第二行 operator:x:11:0:operator:/root:/sbin/nologin [[email protected] sed]# sed -n '2,5'p test.txt 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 [[email protected] sed]# sed -n '2,5'p test.txt 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 [[email protected] 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 [[email protected] sed]# sed -n '1,$'p test.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 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:/root:/sbin/nologin games:x:12:100:games:/usr/games:/sbin/nologin ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin nobody:x:99:99:Nobody:/:/sbin/nologin systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin 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 [[email protected] sed]# sed -e '1'p -e '/bus/'p -n test.txt ## -e 做多个动作,-e 跟动作,匹配,打印 [[email protected] 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 [[email protected] sed]# sed -e '1'p -e '/root/'p -e '/oo\+/'p -n test.txt root:x:0:0:root:/root:/bin/bash root:x:0:0:root:/root:/bin/bash ##相同的行会放在一起 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 operator:x:11:0:operator:/root:/sbin/nologin postfix:x:89:89::/var/spool/postfix:/sbin/nologin [[email protected] sed]# sed -n '/^p/'p test.txt ##以p开头的行 polkitd:x:999:997:User for polkitd:/:/sbin/nologin postfix:x:89:89::/var/spool/postfix:/sbin/nologin [[email protected] sed]# sed -n '/sh$/'p test.txt ##以sh结尾的行 root:x:0:0:root:/root:/bin/bash [[email protected] sed]# sed -n '/r..o/'p test.txt ##匹配r和o之间有两个字符的行 operator:x:11:0:operator:/root:/sbin/nologin polkitd:x:999:997:User for polkitd:/:/sbin/nologin [[email protected] sed]# sed -n '/bus/'Ip test.txt ##大写 I ,忽略大小写 sync:x:5:0:sync:/sbin:/bin/syncBUS dbus:x:81:81:System message bus:/:/sbin/nologin
·删除指定的行(只是在结果中删除)
[[email protected] 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
·删除相关的字符的行
[[email protected] sed]# sed '/nologin/'d test.txt root:x:0:0:root:/root:/bin/bash sync:x:5:0:sync:/sbin:/bin/syncBUS shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt
·替换指定的行:s替换,g全局
[[email protected] sed]# sed '1,10s/nologin/checkout/g' test.txt root:x:0:0:root:/root:/bin/bash ##1-10行,s 替换nologin替换为checkout,g全局显示 bin:x:1:1:bin:/bin:/sbin/checkout daemon:x:2:2:daemon:/sbin:/sbin/checkout adm:x:3:4:adm:/var/adm:/sbin/checkout lp:x:4:7:lp:/var/spool/lpd:/sbin/checkout sync:x:5:0:sync:/sbin:/bin/syncBUS 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/checkout operator:x:11:0:operator:/root:/sbin/checkout games:x:12:100:games:/usr/games:/sbin/nologin ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin nobody:x:99:99:Nobody:/:/sbin/nologin systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin 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
[[email protected] 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/syncBUS 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
[[email protected] 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/syncBUS: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
·分隔符用@ #都可以,这样用/ /的时候就不用加 \ 脱义
[[email protected] sed]# head test.txt | sed 's//sbin/nologin/123/g' ##多个 / 不明其意 sed:-e 表达式 #1,字符 9:“s”的未知选项 [[email protected] sed]# head test.txt | sed 's/\/sbin\/nologin/123/g' ## \ 脱义 root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:123 daemon:x:2:2:daemon:/sbin:123 adm:x:3:4:adm:/var/adm:123 lp:x:4:7:lp:/var/spool/lpd:123 sync:x:5:0:sync:/sbin:/bin/syncBUS shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt mail:x:8:12:mail:/var/spool/mail:123 operator:x:11:0:operator:/root:123 [[email protected] sed]# head test.txt | sed 's#/sbin/nologin#123#g' ##用#做分隔符 root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:123 daemon:x:2:2:daemon:/sbin:123 adm:x:3:4:adm:/var/adm:123 lp:x:4:7:lp:/var/spool/lpd:123 sync:x:5:0:sync:/sbin:/bin/syncBUS shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt mail:x:8:12:mail:/var/spool/mail:123 operator:x:11:0:operator:/root:123 [[email protected] 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::/://
·把开头都替换为aaa (&指之前匹配到的字符)
[[email protected] 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/syncBUS 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 [[email protected] sed]# head test.txt | sed -r 's/(^.*$)/aaa:\1/g' 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/syncBUS 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:/roto:/sbin/nologin
·直接编辑文件 sed -i ,会删除1-15行
[[email protected] sed]# sed -i '1,15'd test.txt
以上是关于2018-1-16 6周2次课 sed的主要内容,如果未能解决你的问题,请参考以下文章