sed -n '/a/'p 2.txt -n只显示匹配的行,'/a/' 匹配内容 p打印linux
[root@glinux-01 ~]# cat 2.txt aaaa bbbb AAAA avavav 123 445ff [root@glinux-01 ~]# sed -n '/aa/'p 2.txt aaaa [root@glinux-01 ~]# sed -n '/a/'p 2.txt aaaa avavav [root@glinux-01 ~]# sed -n '/aa*/'p 2.txt aaaa avavav [root@glinux-01 ~]# sed -n '/aa?/'p 2.txt [root@glinux-01 ~]# sed -n '/a.a/'p 2.txt aaaa avavav
打印指定行bash
[root@glinux-01 ~]# sed -n '2,3'p 2.txt bbbb AAAA [root@glinux-01 ~]# sed -n '2,$'p 2.txt //打印2到最后一行 bbbb AAAA avavav 123 445ff
打印某行到某行之间全部行spa
[root@glinux-01 ~]# sed -n '/ert/,/123/'p lx1.txt ert fff ** [abcfd] 123
-e :选项容许在同一行里执行多条命令code
[root@glinux-01 ~]# sed -n -e '2'p -e '3'p 2.txt bbbb AAAA
I 不区分大小写字符串
[root@glinux-01 ~]# sed -n '/a/'Ip 2.txt aaaa AAAA avavav
d 删除指定行,并将剩余行显示出来(只是屏幕上删除,文件内容并未删除,若是想删除文件加- i)dva
[root@glinux-01 ~]# sed '1,3'd 2.txt avavav 123 445ff [root@glinux-01 ~]# cat 2.txt aaaa bbbb AAAA avavav 123 445ff
s///g 查找替换()扩展
[root@glinux-01 ~]# sed '1,$s/a/A/g' 2.txt//从第一行到最后一行替换a为A AAAA bbbb AAAA AvAvAv 123 445ff
不加g表明只替换每行第一个a换成d 。加g表明替换每行中全部a换成dsed
[root@glinux-01 ~]# sed 's/a/d/g' 2.txt dddd bbbb AAAA dvdvdv 123 445ff [root@glinux-01 ~]# sed 's/a/d/' 2.txt daaa bbbb AAAA dvavav 123 445ff
不用-r的话, | & + (){}要转义的file
不用-r的话, | & + 要转义的 [root@glinux-01 ~]# sed -r '1,6s/a+/c/g' 2.txt c bbbb AAAA cvcvcv 123 445ff
s///g 中,/能够用@#替换,方法
[root@glinux-01 ~]# sed 's//b/c/g' 2.txt sed:-e 表达式 #1,字符 6:“s”的未知选项 [root@glinux-01 ~]# sed 's/\/b/c/g' 2.txt aaaa cbbb AAAA avavav 123 445ff [root@glinux-01 ~]# sed 's@/b@c@g' 2.txt aaaa cbbb AAAA avavav 123 445ff
将每行前加上aaa: (.*)匹配全部 替换为aaa:\1 (\1表明匹配出的第一个字符或用&)
注意:\1 只能匹配前面带()的内容
[root@glinux-01 ~]# sed -r 's/(.*)/aaa:\1/' 2.txt aaa:aaaa aaa:/bbbb aaa:AAAA aaa:avavav aaa:123 aaa:445ff
首列和尾列替换s/([^:]+)(.*:)([^:]+)/\3\2\1/
([^:]+)匹配非:开头内容 root 对应/1
(.*:)匹配以:结尾的内容 :x:0:0:root:/root: 对应/2
([^:]+)匹配非:开头内容 /bin/bash 对应/3
符合匹配规则的内容,在下一次匹配时就不在匹配范围内了
[root@glinux-01 ~]# head pw.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@glinux-01 ~]# head pw.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
sed中,
1. 把每一个单词的第一个小写字母变大写:(&表示前面匹配的内容)
sed 's/\b[a-z]/\u&/g' filename
2. 把全部小写变大写:
sed 's/[a-z]/\u&/g' filename
3. 大写变小写:
sed 's/[A-Z]/\l&/g' filename
4. /b例子:
/\bm/ 匹配 "moon" 中的 'm'; /oo\b/ 不匹配 "moon" 中的 'oo',由于 'oo' 后面的 'n' 是一个单词字符; /oon\b/ 匹配 "moon" 中的 'oon',由于 'oon' 位于字符串的末端,后面没有单词字符; /\w\b\w/ 不匹配任何字符,由于单词字符以后毫不会同时紧跟着非单词字符和单词字符。
5 sed在文件中某一行最后添加一个数字
[root@g_linux01 ~]# sed 's/^e.*/& 12/' lx1.txt //匹配以e开头的词,并在其后加 12 ert 12 fff ** [abcfd] 123 324 444 [rty] ** fgfgf [] ____ !@# eq 12 qe er 12 方法2: [root@g_linux01 ~]# sed '/^e/s/$/ 12/' lx1.txt //匹配以e开头的词,在末尾添加 12 (^表示在开头$末尾) ert 12 fff ** [abcfd] 123 324 444 [rty] ** fgfgf [] ____ !@# eq 12 qe er 12
打印1到100行包含某个字符串的行
sed -n '1,100{/abc/p}' 1.txt