码迷,mamicode.com
首页 > 系统相关 > 详细

2018-03-13 Linux学习

时间:2018-03-13 21:09:10      阅读:193      评论:0      收藏:0      [点我收藏+]

标签:Linux   学习   

9.4 sed (上)

sed 工具 示例
    sed -n ‘5‘p test.txt
    sed -n ‘1,5‘p test.txt
    sed -n ‘1,$‘p test.txt
    sed -n ‘/root/‘p test.txt
    sed -n ‘/^1/‘p test.txt
    sed -n ‘in$‘p test.txt
    sed -n ‘/r..o/‘p test.txt
    sed -n ‘oo*‘p test.txt
    sed -e ‘1‘p -e ‘/111/‘p -n test.txt

    [root@aming-01 sed]# sed -n ‘/root/‘p test.txt 
    root:x:0:0:root:/root:/bin/bash
    operator:x:11:0:operator:/root:/sbin/nologin

    [root@aming-01 sed]# sed -nr ‘/o+t/‘p test.txt 
    root:x:0:0:root:/root:/bin/bash
    operator:x:11:0:operator:/root:/sbin/nologin

    [root@aming-01 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

    [root@aming-01 sed]# sed -n ‘1,$‘p test.txt

    [root@aming-01 sed]# sed -n ‘/^s/‘p test.txt 
    sync:x:5:0:sync:/sbin:/bin/sync
    shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
    systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
    sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin

    [root@aming-01 sed]# sed -e ‘1‘p -e ‘/bus/‘p -n test.txt 
    root:x:0:0:root:/root:/bin/bash
    dbus:x:81:81:System message bus:/:/sbin/nologin

    [root@aming-01 sed]# sed -e ‘1‘p -e ‘/bus/‘p -e ‘/lp/‘p -n test.txt 
    root:x:0:0:root:/root:/bin/bash
    lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
    dbus:x:81:81:System message bus:/:/sbin/nologin

9.5 sed (下)

-n   使用安静(silent)模式。在一般 sed 的用法中,所有来自 STDIN 的数据一般都会被列出到屏幕上。但如果加上 -n 参数后,则只有经过 sed 特殊处理的那一行(或者动作)才会被列出来。
-p   打印
-I   不区分大小写
-d   删除
-i   修改文件内容
-r   加 -r 特殊符号不需要加 \ 脱义
s/a/b/g  全部替换
s/a/b/   替换第一个匹配的
&    代表前面的小括号

    sed 示例
    sed ‘1‘d test.txt
    sed ‘1,10‘d test.txt
    sed ‘/oot/‘d test.txt
    sed ‘1,2s/ot/to/g‘ test.txt
    sed ‘s#ot#ss#g‘ test.txt
    sed ‘s/[0-9]//g‘ test.txt
    sed ‘s/[a-zA-Z]//g‘ test.txt
    sed -r ‘s/(rot)(.*)(bash)/\3\2\1/‘ test.txt
    sed ‘s/^.*$/123&/‘ test.txt
    sed -i ‘s/ot/ss/g‘ test.txt

        [root@aming-01 sed]# sed -n ‘/user/‘Ip test.txt 
        ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
        polkitd:x:999:997:User for polkitd:/:/sbin/nologin

        [root@aming-01 sed]# sed ‘1,18‘d test.txt 
        bacula:x:133:133:Bacula Backup System:/var/spool/bacula:/sbin/nologin
        apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin

        [root@aming-01 sed]# head -3 test.txt|sed -r ‘s/roo/aaa/‘
        aaat:x:0:0:root:/root:/bin/bash
        bin:x:1:1:bin:/bin:/sbin/nologin
        daemon:x:2:2:daemon:/sbin:/sbin/nologin

        [root@aming-01 sed]# head -3 test.txt|sed -r ‘s/roo/aaa/g‘
        aaat:x:0:0:aaat:/aaat:/bin/bash
        bin:x:1:1:bin:/bin:/sbin/nologin
        daemon:x:2:2:daemon:/sbin:/sbin/nologin

        [root@aming-01 sed]# sed ‘s#ot#ss#g‘ test.txt 
        ross:x:0:0:ross:/ross:/bin/bash

        [root@aming-01 sed]# sed ‘s/[0-9]//g‘ test.txt 
        root:x:::root:/root:/bin/bash
        bin:x:::bin:/bin:/sbin/nologin

        [root@aming-01 sed]# sed ‘s/[a-zA-Z]//g‘ test.txt 
        ::0:0::/://
        ::1:1::/://
        ::2:2::/://

        [root@aming-01 sed]# head -3 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

        [root@aming-01 sed]# head -3 test.txt|sed -r ‘s/(.*)/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

2018-03-13 Linux学习

标签:Linux   学习   

原文地址:http://blog.51cto.com/9298822/2086197

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!