9.4 sed(上)
过滤字符:
-
[root@localhost sed]# sed -n '/root/'p passwd
-
root:x:0:0:root:/root:/bin/bash
-
operator:x:11:0:operator:/root:/sbin/nologin
也支持“.” “*”的匹配:
-
[root@localhost sed]# sed -n '/r.t/'p passwd
-
operator:x:11:0:operator:/root:/sbin/nologin
-
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
-
[root@localhost sed]# sed -n '/r*t/'p passwd
-
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/spool/postfix:/sbin/nologin
-
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
-
haha:x:1007:1005::/home/test:/sbin/nologin
-
test1:x:1100:1009::/home/test1:/bin/bash
-
saslauth:x:997:76:Saslauthd user:/run/saslauthd:/sbin/nologin
还有“+”的匹配:不过“+”符号需要转义,或者使用-r选项就不需要转义了。两者都没有,则无法匹配到内容。
-
[root@localhost sed]# sed -n '/o\+t/'p passwd
-
root:x:0:0:root:/root:/bin/bash
-
operator:x:11:0:operator:/root:/sbin/nologin
-
[root@localhost sed]# sed -nr '/o+t/'p passwd
-
root:x:0:0:root:/root:/bin/bash
-
operator:x:11:0:operator:/root:/sbin/nologin
相同字符匹配多次:
-
[root@localhost sed]# sed -nr '/o{2}/'p passwd
-
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]# sed -nr '/root|bus/'p passwd
-
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
sed下
查看具体某行的内容:
-
[root@localhost sed]# sed -n '2'p passwd
-
bin:x:1:1:bin:/bin:/sbin/nologin
-
[root@localhost sed]# sed -n '13'p passwd
-
nobody:x:99:99:Nobody:/:/sbin/nologin
-
[root@localhost sed]# sed -n '1,3'p passwd
-
root:x:0:0:root:/root:/bin/bash
-
bin:x:1:1:bin:/bin:/sbin/nologin
-
daemon:x:2:2:daemon:/sbin:/sbin/nologin
-
[root@localhost sed]# sed -n '29,$'p passwd
-
aiya:x:1102:1102::/www/aiya:/sbin/nologin
-
dhcpd:x:177:177:DHCP server:/:/sbin/nologin
-
saslauth:x:997:76:Saslauthd user:/run/saslauthd:/sbin/nologin
同时完成两个操作:当两个内容想重合,那么会重复两次:
-
[root@localhost sed]# sed -e '1'p -e '/bus/'p -n passwd
-
root:x:0:0:root:/root:/bin/bash
-
dbus:x:81:81:System message bus:/:/sbin/nologin
内容重合:
-
[root@localhost sed]# sed -e '1'p -e '/root/'p -n passwd
-
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]# sed -n '/roo*/'p passwd //有ro即可。
-
root:x:0:0:root:/root:/bin/bash
-
operator:x:11:0:operator:/root:/sbin/nologin
-
chrony:x:998:996::/var/lib/chrony:/sbin/nologin
阅读(1006) | 评论(0) | 转发(0) |