Chinaunix首页 | 论坛 | 博客
  • 博客访问: 140413
  • 博文数量: 70
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 770
  • 用 户 组: 普通用户
  • 注册时间: 2017-11-04 11:19
文章分类

全部博文(70)

文章存档

2018年(69)

2016年(1)

我的朋友

分类: LINUX

2018-07-05 23:40:44

9.4 sed(上)
过滤字符:

点击(此处)折叠或打开

  1. [root@localhost sed]# sed -n '/root/'p passwd
  2. root:x:0:0:root:/root:/bin/bash
  3. operator:x:11:0:operator:/root:/sbin/nologin
也支持“.” “*”的匹配:

点击(此处)折叠或打开

  1. [root@localhost sed]# sed -n '/r.t/'p passwd
  2. operator:x:11:0:operator:/root:/sbin/nologin
  3. sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
  4. [root@localhost sed]# sed -n '/r*t/'p passwd
  5. root:x:0:0:root:/root:/bin/bash
  6. shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
  7. halt:x:7:0:halt:/sbin:/sbin/halt
  8. operator:x:11:0:operator:/root:/sbin/nologin
  9. ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
  10. systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
  11. dbus:x:81:81:System message bus:/:/sbin/nologin
  12. polkitd:x:999:997:User for polkitd:/:/sbin/nologin
  13. postfix:x:89:89::/var/spool/postfix:/sbin/nologin
  14. sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
  15. haha:x:1007:1005::/home/test:/sbin/nologin
  16. test1:x:1100:1009::/home/test1:/bin/bash
  17. saslauth:x:997:76:Saslauthd user:/run/saslauthd:/sbin/nologin
还有“+”的匹配:不过“+”符号需要转义,或者使用-r选项就不需要转义了。两者都没有,则无法匹配到内容。

点击(此处)折叠或打开

  1. [root@localhost sed]# sed -n '/o\+t/'p passwd
  2. root:x:0:0:root:/root:/bin/bash
  3. operator:x:11:0:operator:/root:/sbin/nologin
  4. [root@localhost sed]# sed -nr '/o+t/'p passwd
  5. root:x:0:0:root:/root:/bin/bash
  6. operator:x:11:0:operator:/root:/sbin/nologin

相同字符匹配多次:

点击(此处)折叠或打开

  1. [root@localhost sed]# sed -nr '/o{2}/'p passwd
  2. root:x:0:0:root:/root:/bin/bash
  3. lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
  4. mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
  5. operator:x:11:0:operator:/root:/sbin/nologin
  6. postfix:x:89:89::/var/spool/postfix:/sbin/nologin

或者选项:

点击(此处)折叠或打开

  1. [root@localhost sed]# sed -nr '/root|bus/'p passwd
  2. root:x:0:0:root:/root:/bin/bash
  3. operator:x:11:0:operator:/root:/sbin/nologin
  4. dbus:x:81:81:System message bus:/:/sbin/nologin

sed下
查看具体某行的内容:

点击(此处)折叠或打开

  1. [root@localhost sed]# sed -n '2'p passwd
  2. bin:x:1:1:bin:/bin:/sbin/nologin
  3. [root@localhost sed]# sed -n '13'p passwd
  4. nobody:x:99:99:Nobody:/:/sbin/nologin
  5. [root@localhost sed]# sed -n '1,3'p passwd
  6. root:x:0:0:root:/root:/bin/bash
  7. bin:x:1:1:bin:/bin:/sbin/nologin
  8. daemon:x:2:2:daemon:/sbin:/sbin/nologin
  9. [root@localhost sed]# sed -n '29,$'p passwd
  10. aiya:x:1102:1102::/www/aiya:/sbin/nologin
  11. dhcpd:x:177:177:DHCP server:/:/sbin/nologin
  12. saslauth:x:997:76:Saslauthd user:/run/saslauthd:/sbin/nologin

同时完成两个操作:当两个内容想重合,那么会重复两次:

点击(此处)折叠或打开

  1. [root@localhost sed]# sed -e '1'p -e '/bus/'p -n passwd
  2. root:x:0:0:root:/root:/bin/bash
  3. dbus:x:81:81:System message bus:/:/sbin/nologin
内容重合:

点击(此处)折叠或打开

  1. [root@localhost sed]# sed -e '1'p -e '/root/'p -n passwd
  2. root:x:0:0:root:/root:/bin/bash
  3. root:x:0:0:root:/root:/bin/bash
  4. operator:x:11:0:operator:/root:/sbin/nologin

“*”星号通配符:

点击(此处)折叠或打开

  1. [root@localhost sed]# sed -n '/roo*/'p passwd    //有ro即可。
  2. root:x:0:0:root:/root:/bin/bash
  3. operator:x:11:0:operator:/root:/sbin/nologin
  4. chrony:x:998:996::/var/lib/chrony:/sbin/nologin



 












































阅读(952) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~