linux 学习笔记-027-sed

发布于 2018-01-30  271 次阅读


sed

准备工作:

root@am-01:~# mkdir sed

root@am-01:~# cp grep/passwd sed/test.txt

root@am-01:~# ls sed/

test.txt

root@am-01:~# cd sed/

root@am-01:~/sed# pwd

/root/sed

匹配带有 root 的行(-n)

root@am-01:~/sed# sed -n '/root/'p test.txt

root:x:0:0:rooot:/root:/bin/bash

operator:x:11:0:operator:/root:/sbin/nologin

例子 o+t,匹配+号左边的字符 1 次或多次的行打印出来(-r)

root@am-01:~/sed# sed -nr '/o+t/'p test.txt

root:x:0:0:rooot:/root:/bin/bash

operator:x:11:0:operator:/root:/sbin/nologin

例子 o{2},匹配带有连续两个 o 的行

root@am-01:~/sed# sed -nr '/o{2}/'p test.txt

root:x:0:0:rooot:/root:/bin/bash

sadas:r.oooo

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 或者 bus 的行

root@am-01:~/sed# sed -nr '/root|bus/'p test.txt

root:x:0:0:rooot:/root:/bin/bash

operator:x:11:0:operator:/root:/sbin/nologin

dbus:x:81:81:System message bus:/:/sbin/nologin

打印第二行和打印第二行到第五行

root@am-01:~/sed# sed -n '2'p test.txt

bin:x:1:1:bin:/bin:/sbin/NOLogin

root@am-01:~/sed# sed -n '2,5'p test.txt

bin:x:1:1:bin:/bin:/sbin/NOLogin

sadas:r.oooo

dsgfgf:r1o:

daemon:x:2:2:daemon:/sbin:/sbin/nologin

打印第二十行到最后一行,$表示末行

root@am-01:~/sed# sed -n '20,$'p test.txt

postfix:x:89:89::/var/spool/postfix:/sbin/nologin

sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin

aming:x:1000:1000::/home/aming:/bin/bash

ntp:x:38:38::/etc/ntp:/sbin/nologin

user1:x:1001:1001::/home/user1:/bin/bash

user2:x:1002:1002::/home/user2:/bin/bash

user3:x:1003:1005::/home/user3:/bin/bash

user4:x:1004:1005::/home/aming111:/sbin/nologin

user5:x:1005:1006::/home/user5:/bin/bash

user6:x:1007:1007::/home/user6:/bin/bash

同一个表达式做多个操作,可以加-e 参数

root@am-01:~/sed# sed -e '/root/'p -e '1'p -n test.txt

root:x:0:0:rooot:/root:/bin/bash

root:x:0:0:rooot:/root:/bin/bash

operator:x:11:0:operator:/root:/sbin/nologin

匹配的时候不区分大小写

root@am-01:~/sed# vim test.txt    #随便一行添加大写的 BUS,便于做实验

dsgfgf:r1o:BUS

root@am-01:~/sed# sed -n '/bus/'Ip test.txt

dsgfgf:r1o:BUS

dbus:x:81:81:System message bus:/:/sbin/nologin

把文件除了 1-25 行之外的都匹配出来,d 是删除的意思(不会对文件本身做操作)

root@am-01:~/sed# sed '1,25'd test.txt

user3:x:1003:1005::/home/user3:/bin/bash

user4:x:1004:1005::/home/aming111:/sbin/nologin

user5:x:1005:1006::/home/user5:/bin/bash

user6:x:1007:1007::/home/user6:/bin/bash

选项-i 会删除文件中指定的内容(会对文件本身做操作)

root@am-01:~/sed# sed -i '1,25'd test.txt

root@am-01:~/sed# wc -l test.txt

4 test.txt

指定删除含有某个关键字的一行(会对文件本身做操作)

root@am-01:~/sed# sed -i '/user3/'d test.txt

root@am-01:~/sed# cat test.txt

user4:x:1004:1005::/home/aming111:/sbin/nologin

user5:x:1005:1006::/home/user5:/bin/bash

user6:x:1007:1007::/home/user6:/bin/bash

查找 1-10 行的 root,替换为 toor

root@am-01:~/sed# sed '1,10s/root/toor/g' test.txt | head

toor:x:0:0:rooot:/toor:/bin/bash

bin:x:1:1:bin:/bin:/sbin/NOLogin

sadas:r.oooo

dsgfgf:r1o:

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

查找 1-10 行,无论+号左边的字符 ro 出现过 1 次或多次,都把它替换为 r(切记在 sed 中有+号需要用-r 参数)

root@am-01:~/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

sadas:r.oooo

dsgfgf:r1o:

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

把 1-10 行的行首和行尾替换位置

这里每一个括号的内容跟后面的 1/2/3 相对应,其中(.*):指的是贪婪匹配到最后一个冒号的位置

root@am-01:~/sed# head test.txt| sed -r 's/([^:]+):(.*):([^:]+)/\3:\2:\1/g'

/bin/bash:x:0:0:rooot:/root:root

/sbin/NOLogin:x:1:1:bin:/bin:bin

sadas:r.oooo

dsgfgf:r1o:

/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

如果有多个斜杠或特殊符号就需要脱义

root@am-01:~/sed# head test.txt| sed 's/\/sbin\/nologin/123/g'

root:x:0:0:rooot:/root:/bin/bash

bin:x:1:1:bin:/bin:/sbin/NOLogin

sadas:r.oooo

dsgfgf:r1o:

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/sync

shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown

halt:x:7:0:halt:/sbin:/sbin/halt

root@am-01:~/sed# head test.txt| sed 's@/sbin/nologin@123@g'

root:x:0:0:rooot:/root:/bin/bash

bin:x:1:1:bin:/bin:/sbin/NOLogin

sadas:r.oooo

dsgfgf:r1o:

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/sync

shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown

halt:x:7:0:halt:/sbin:/sbin/halt

把英文字母都删除掉(不会对文件本身做操作)

root@am-01:~/sed# head test.txt | sed 's/[a-zA-Z]//g'

::0:0::/://

::1:1::/://

:.

:1:

::2:2::/://

::3:4:://://

::4:7::///://

::5:0::/://

::6:0::/://

::7:0::/://

在所有行前面加上固定的字符(\1 和&的意思一样,有特殊符号需要加-r)

root@am-01:~/sed# head test.txt | sed -r 's/(.*)/aaa:\1/g'

aaa:root:x:0:0:rooot:/root:/bin/bash

aaa:bin:x:1:1:bin:/bin:/sbin/NOLogin

aaa:sadas:r.oooo

aaa:dsgfgf:r1o:

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

root@am-01:~/sed# head test.txt | sed -r 's/(.*)/aaa:&/g'

aaa:root:x:0:0:rooot:/root:/bin/bash

aaa:bin:x:1:1:bin:/bin:/sbin/NOLogin

aaa:sadas:r.oooo

aaa:dsgfgf:r1o:

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