• 文本处理工具——sed基础


    一sed介绍

    三剑客是grep,sed,awk,功能都很强大。

    其中sed是Stream EDitor,流编辑器 行,编辑器的简写,它一次处理一行内容。

    sed的强大在于可以对文件进行修改,很适合在脚本中使用,就不需要在vim中修改文件。

    处理时把当前处理的行存储在临时缓冲区中,称为“模式空间”(patternspace),

    接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏幕。


    然后读入下行,执行下一个循环。如果没有使诸如‘D’的特殊命令,那会在两个循环之间清空模式空间,但不会清空保留空间。



    这样不断重复,直到文件末尾。文件内容并没有改变,除非你使用重定向存储输出。


    功能:主要用来自动编辑一个或多个文件,简化对文件的反复操作,编写转换程序等。

    官方参考文档:http://www.gnu.org/software/sed/manual/sed.html

    默认是基于GNU项目开发出来的软件

    二sed的工作原理

    sed会自动在内存开辟一块空间给自己使用,这块内存空间叫做模式空间。

    一个文件里面有很多行,sed每次读取文件中的一行,读取到自己的内存空间,还可以根据sed指令对一行行进行切割。

    处理完了一行,这行就没有了,接着读取下面的行内容。

    上面的处理是全自动的,相当于其内置了循环。

    三sed工具的使用

    (一)用法


    sed   [option]...    'script'    inputfile...

    注意脚本不是shell脚本,而是sed自带的语言。

    脚本的格式由两部分组合   script:'地址 命令'

    地址就是文件的行,读入一行要看是否符合地址定义的行。

    最简单的就是不写地址,只有命令。

    (二)常用选项


    -n:不输出模式空间内容到屏幕,即不自动打印

    n是silent的简写

    -n, --quiet, --silent

    suppress automatic printing of pattern space

    -e: 多点编辑

     -e script, --expression=script

                  add the script to the commands to be executed
    指定pattern 和对应的 command


    -f:/ PATH/SCRIPT_FILE : 从指定文件中读取编辑脚本

     -f script-file, --file=script-file

                  add the contents of script-file to the commands to be executed


    -r: 支持使用扩展正则表达式

     -r, --regexp-extended

                  use extended regular expressions in the script.


    -i.bak: 备份文件并原处编辑

       -i[SUFFIX], --in-place[=SUFFIX]

                  edit files in place (makes backup if SUFFIX supplied)

    (三)编辑命令

    查看帮助文档

     
    d: 删除模式空间匹配的行,并立即启用下一轮循环

    d      Delete pattern space.  Start next cycle.


    p:打印当前模式空间内容,追加到默认输出之后

    p Print the current pattern space.

    P Print up to the first embedded newline of the current pattern space.


    a []text:在指定行后面追加文本支持使用 实现多行追加

     a   text   Append text, which has each embedded newline preceded by a backslash.



    i []text:在行前面插入文本

     i   text   Insert text, which has each embedded newline preceded by a backslash.



    c []text:替换行为单行或多行文本

      c

           text   Replace the selected lines with text, which has each embedded newline preceded by a backslash.



    w /path/somefile: 保存模式匹配的行至指定文件

     w filename
                  Write the current pattern space to filename.



    r /path/somefile:读取指定文件的文本至模式空间中匹配到的行后

    r filename
                  Append text read from filename.



    =: 为模式空间中的行打印行号

      =      Print the current line number.


    !:模式空间中匹配行取反处理

    (四)脚本格式介绍

    脚本是由地址和命令组合成的。

    地址就是文件的行,读入一行要看是否符合地址定义的行。最简单是不写地址,只有命令。

    1地址定界


    (1) 不给地址:对全文进行处理,地址就相当于过滤条件。

    就像你是人,指的是全地球人类的一员。

    1)默认就会自动打印,所以是打印两遍。

    [root@centos72 ~]# sed   'p'   /etc/issue
    S
    S
    Kernel 
     on an m
    Kernel 
     on an m

    不加引号也可以

    [root@centos65 ~]# sed   p   /etc/issue
    CentOS release 6.8 (Final)
    CentOS release 6.8 (Final)
    Kernel 
     on an m
    Kernel 
     on an m

    2)自动打印关闭

    把自动打印关闭,现在就打印1遍。

    注意-n只要不加在命令前面即可。

    -n, --quiet, --silent

    suppress automatic printing of pattern space

    p Print the current pattern space.

    P Print up to the first embedded newline of the current pattern space.

    [root@centos72 ~]# sed   'p'   /etc/issue  -n
    S
    Kernel 
     on an m
    
    [root@centos72 ~]# sed    -n   'p'   /etc/issue  
    S
    Kernel 
     on an m

    上面单纯的打印没有什么实际意义,因为cat也可以实现

    [root@centos72 ~]# cat  /etc/issue
    S
    Kernel 
     on an m


    (2)单地址:
    #: 指定的行,$:最后一行,/pattern/:被此处模式所能够匹配到的每一行

    1)#=2表示文件的第2行打印

    [root@centos72 ~]# sed      '2p'   /etc/issue  
    S
    Kernel 
     on an m
    Kernel 
     on an m
    
    [root@centos72 ~]# sed    -n   '2p'   /etc/issue  
    Kernel 
     on an m

    2)地址为空并且自动打印,那么就是把文件内容全部打印一遍

    [root@centos72 ~]# sed  ''  /etc/issue
    S
    Kernel 
     on an m

    3)使用sed读取键盘输入的内容

    因为把自动打印关了,只对第2行打印了1遍

    [root@centos72 ~]# sed  -n  '2p'
    aaaaa
    bbbbb
    #输入的内容 bbbbb
    #自动打印的 ccccc ddddd

    4)

    sed默认是支持标准输入,可以使用管道传输,适合显示命令结果的第几行。

    这种方法可以取代head,tail。很多以前麻烦的事,sed都可以解决的。

    方法多了就可以找到和使用更好的方法,这和学数学一样,解题方法不只一种,发散思维的前提是对知识的积累和掌握。

    显示网络接口信息

    [root@centos72 ~]# ifconfig 
    ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
            inet 192.168.137.72  netmask 255.255.255.0  broadcast 192.168.137.255
            inet6 fe80::b029:2522:876f:5456  prefixlen 64  scopeid 0x20<link>
            ether 00:0c:29:fc:69:f8  txqueuelen 1000  (Ethernet)
            RX packets 1889  bytes 178723 (174.5 KiB)
            RX errors 0  dropped 0  overruns 0  frame 0
            TX packets 1118  bytes 173152 (169.0 KiB)
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
    
    lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
            inet 127.0.0.1  netmask 255.0.0.0
            inet6 ::1  prefixlen 128  scopeid 0x10<host>
            loop  txqueuelen 1000  (Local Loopback)
            RX packets 0  bytes 0 (0.0 B)
            RX errors 0  dropped 0  overruns 0  frame 0
            TX packets 0  bytes 0 (0.0 B)
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

    打印标准输入网络接口信息的第2行内容

    [root@centos72 ~]# ifconfig   |  sed    -n  '2p'
            inet 192.168.137.72  netmask 255.255.255.0  broadcast 192.168.137.255

     5)

    $:最后一行

    打印文件最后一行的信息

    [root@centos72 ~]# sed  ''  /etc/fstab 
    
    #
    # /etc/fstab
    # Created by anaconda on Sun Jan 13 00:14:21 2019
    #
    # Accessible filesystems, by reference, are maintained under '/dev/disk'
    # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
    #
    UUID=5998ead0-b370-4859-9153-ecf4e2b9dd84 /                       xfs     defaults        0 0
    UUID=ac6bb7e3-fa78-4eb2-b00d-e85c421c1bb0 /app                    xfs     defaults        0 0
    UUID=92886c3f-42a3-40f4-8cf7-c6890ca3a52e /boot                   xfs     defaults        0 0
    UUID=104520e1-0e97-4248-8fd0-a21e7d88a881 swap                    swap    defaults        0 0
    /dev/cdrom  /mnt   iso9660  defaults 0 0
    [root@centos72 ~]# sed    -n  '$p'    /etc/fstab 
    /dev/cdrom  /mnt   iso9660  defaults 0 0

     6)

    /pattern/:被此处模式所能够匹配到的每一行,模式支持正则表达式

    打印文件以U开头的所有行

    [root@centos72 ~]#  sed   -n    '/^U/p'    /etc/fstab 
    UUID=5998ead0-b370-4859-9153-ecf4e2b9dd84 /                       xfs     defaults        0 0
    UUID=ac6bb7e3-fa78-4eb2-b00d-e85c421c1bb0 /app                    xfs     defaults        0 0
    UUID=92886c3f-42a3-40f4-8cf7-c6890ca3a52e /boot                   xfs     defaults        0 0
    UUID=104520e1-0e97-4248-8fd0-a21e7d88a881 swap                    swap    defaults        0 0

    /pattern/前后不加引号也可以

    [root@centos72 ~]#  sed   -n    /^U/p    /etc/fstab 
    UUID=5998ead0-b370-4859-9153-ecf4e2b9dd84 /                       xfs     defaults        0 0
    UUID=ac6bb7e3-fa78-4eb2-b00d-e85c421c1bb0 /app                    xfs     defaults        0 0
    UUID=92886c3f-42a3-40f4-8cf7-c6890ca3a52e /boot                   xfs     defaults        0 0
    UUID=104520e1-0e97-4248-8fd0-a21e7d88a881 swap                    swap    defaults        0 0

    注意地址后面一定要加上命令,否则会报错

    [root@centos72 ~]# sed   '/^U/'    /etc/fstab 
    sed: -e expression #1, char 4: missing command

    grep更方便

    [root@centos72 ~]#  grep   ^U  /etc/fstab 
    UUID=5998ead0-b370-4859-9153-ecf4e2b9dd84 /                       xfs     defaults        0 0
    UUID=ac6bb7e3-fa78-4eb2-b00d-e85c421c1bb0 /app                    xfs     defaults        0 0
    UUID=92886c3f-42a3-40f4-8cf7-c6890ca3a52e /boot                   xfs     defaults        0 0
    UUID=104520e1-0e97-4248-8fd0-a21e7d88a881 swap                    swap    defaults        0 0
    [root@centos72 ~]#  grep   '^U'  /etc/fstab 
    UUID=5998ead0-b370-4859-9153-ecf4e2b9dd84 /                       xfs     defaults        0 0
    UUID=ac6bb7e3-fa78-4eb2-b00d-e85c421c1bb0 /app                    xfs     defaults        0 0
    UUID=92886c3f-42a3-40f4-8cf7-c6890ca3a52e /boot                   xfs     defaults        0 0
    UUID=104520e1-0e97-4248-8fd0-a21e7d88a881 swap                    swap    defaults        0 0

     
    (3)地址范围,也就是第几行到第几行
    #,#
    #,+#
    /pat1/,/pat2/
    #,/pat1/

    1)

    #,#

    nl   /etc/fstab 显示此文件的所有行号

    [root@centos72 ~]# nl  /etc/fstab 
           
         1    #
         2    # /etc/fstab
         3    # Created by anaconda on Sun Jan 13 00:14:21 2019
         4    #
         5    # Accessible filesystems, by reference, are maintained under '/dev/disk'
         6    # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
         7    #
         8    UUID=5998ead0-b370-4859-9153-ecf4e2b9dd84 /                       xfs     defaults        0 0
         9    UUID=ac6bb7e3-fa78-4eb2-b00d-e85c421c1bb0 /app                    xfs     defaults        0 0
        10    UUID=92886c3f-42a3-40f4-8cf7-c6890ca3a52e /boot                   xfs     defaults        0 0
        11    UUID=104520e1-0e97-4248-8fd0-a21e7d88a881 swap                    swap    defaults        0 0
        12    /dev/cdrom  /mnt   iso9660  defaults 0 0
    [root@centos72 ~]# sed  -n  '8,12p'   /etc/fstab 
    #
    UUID=5998ead0-b370-4859-9153-ecf4e2b9dd84 /                       xfs     defaults        0 0
    UUID=ac6bb7e3-fa78-4eb2-b00d-e85c421c1bb0 /app                    xfs     defaults        0 0
    UUID=92886c3f-42a3-40f4-8cf7-c6890ca3a52e /boot                   xfs     defaults        0 0
    UUID=104520e1-0e97-4248-8fd0-a21e7d88a881 swap                    swap    defaults        0 0

    sed的行号是包括空行的

    使用cat -n打印行号更准确。

    [root@centos72 ~]# cat  -n  /etc/fstab 
         1    
         2    #
         3    # /etc/fstab
         4    # Created by anaconda on Sun Jan 13 00:14:21 2019
         5    #
         6    # Accessible filesystems, by reference, are maintained under '/dev/disk'
         7    # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
         8    #
         9    UUID=5998ead0-b370-4859-9153-ecf4e2b9dd84 /                       xfs     defaults        0 0
        10    UUID=ac6bb7e3-fa78-4eb2-b00d-e85c421c1bb0 /app                    xfs     defaults        0 0
        11    UUID=92886c3f-42a3-40f4-8cf7-c6890ca3a52e /boot                   xfs     defaults        0 0
        12    UUID=104520e1-0e97-4248-8fd0-a21e7d88a881 swap                    swap    defaults        0 0
        13    /dev/cdrom  /mnt   iso9660  defaults 0 0
    [root@centos72 ~]# sed  -n  '9,13p'   /etc/fstab 
    UUID=5998ead0-b370-4859-9153-ecf4e2b9dd84 /                       xfs     defaults        0 0
    UUID=ac6bb7e3-fa78-4eb2-b00d-e85c421c1bb0 /app                    xfs     defaults        0 0
    UUID=92886c3f-42a3-40f4-8cf7-c6890ca3a52e /boot                   xfs     defaults        0 0
    UUID=104520e1-0e97-4248-8fd0-a21e7d88a881 swap                    swap    defaults        0 0
    /dev/cdrom  /mnt   iso9660  defaults 0 0
    [root@centos72 ~]# sed  -n  '9,12p'   /etc/fstab 
    UUID=5998ead0-b370-4859-9153-ecf4e2b9dd84 /                       xfs     defaults        0 0
    UUID=ac6bb7e3-fa78-4eb2-b00d-e85c421c1bb0 /app                    xfs     defaults        0 0
    UUID=92886c3f-42a3-40f4-8cf7-c6890ca3a52e /boot                   xfs     defaults        0 0
    UUID=104520e1-0e97-4248-8fd0-a21e7d88a881 swap                    swap    defaults        0 0

    2)

    #,+#  从第几行到本行之后的第几行

    [root@centos72 ~]# cat  -n  /etc/fstab 
         1    
         2    #
         3    # /etc/fstab
         4    # Created by anaconda on Sun Jan 13 00:14:21 2019
         5    #
         6    # Accessible filesystems, by reference, are maintained under '/dev/disk'
         7    # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
         8    #
         9    UUID=5998ead0-b370-4859-9153-ecf4e2b9dd84 /                       xfs     defaults        0 0
        10    UUID=ac6bb7e3-fa78-4eb2-b00d-e85c421c1bb0 /app                    xfs     defaults        0 0
        11    UUID=92886c3f-42a3-40f4-8cf7-c6890ca3a52e /boot                   xfs     defaults        0 0
        12    UUID=104520e1-0e97-4248-8fd0-a21e7d88a881 swap                    swap    defaults        0 0
        13    /dev/cdrom  /mnt   iso9660  defaults 0 0
    [root@centos72 ~]# sed  -n  '9,+3p'   /etc/fstab 
    UUID=5998ead0-b370-4859-9153-ecf4e2b9dd84 /                       xfs     defaults        0 0
    UUID=ac6bb7e3-fa78-4eb2-b00d-e85c421c1bb0 /app                    xfs     defaults        0 0
    UUID=92886c3f-42a3-40f4-8cf7-c6890ca3a52e /boot                   xfs     defaults        0 0
    UUID=104520e1-0e97-4248-8fd0-a21e7d88a881 swap                    swap    defaults        0 0

    注意只能+,不能-

    [root@centos72 ~]# sed  -n  '9,-3p'   /etc/fstab 
    sed: -e expression #1, char 3: unexpected `,'

    3)/pat1/,/pat2/  打印两个模式之间的内容,也就要匹配两个条件

    [root@centos72 ~]# sed  ''    /etc/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
    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
    games:x:12:100:games:/usr/games:/sbin/nologin
    ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
    nobody:x:99:99:Nobody:/:/sbin/nologin
    systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
    dbus:x:81:81:System message bus:/:/sbin/nologin
    polkitd:x:999:998:User for polkitd:/:/sbin/nologin
    sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
    postfix:x:89:89::/var/spool/postfix:/sbin/nologin
    wang:x:1000:1000:wang:/home/wang:/bin/bash

    b开头h结尾的行

    [root@centos72 ~]# sed  -n   /^b/,/^h/p   /etc/passwd
    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
    [root@centos72 ~]# sed  -n   '/^b/,/^h/p'   /etc/passwd
    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

     行以r开头u结尾,但是没有u结尾就打印到文件的最后一行

    [root@centos72 ~]# cat  /etc/passwd   |  sed  -n   '/^r/,/^u/p'
    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
    games:x:12:100:games:/usr/games:/sbin/nologin
    ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
    nobody:x:99:99:Nobody:/:/sbin/nologin
    systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
    dbus:x:81:81:System message bus:/:/sbin/nologin
    polkitd:x:999:998:User for polkitd:/:/sbin/nologin
    sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
    postfix:x:89:89::/var/spool/postfix:/sbin/nologin
    wang:x:1000:1000:wang:/home/wang:/bin/bash

    4)#,/pat1/

    从第几行到匹配模式的行

    [root@centos72 ~]# sed  -n   '6,/^u/p'   /etc/passwd
    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
    games:x:12:100:games:/usr/games:/sbin/nologin
    ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
    nobody:x:99:99:Nobody:/:/sbin/nologin
    systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
    dbus:x:81:81:System message bus:/:/sbin/nologin
    polkitd:x:999:998:User for polkitd:/:/sbin/nologin
    sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
    postfix:x:89:89::/var/spool/postfix:/sbin/nologin
    wang:x:1000:1000:wang:/home/wang:/bin/bash
    [root@centos72 ~]# cat    -n   /etc/passwd     
         1    root:x:0:0:root:/root:/bin/bash
         2    bin:x:1:1:bin:/bin:/sbin/nologin
         3    daemon:x:2:2:daemon:/sbin:/sbin/nologin
         4    adm:x:3:4:adm:/var/adm:/sbin/nologin
         5    lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
         6    sync:x:5:0:sync:/sbin:/bin/sync
         7    shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
         8    halt:x:7:0:halt:/sbin:/sbin/halt
         9    mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
        10    operator:x:11:0:operator:/root:/sbin/nologin
        11    games:x:12:100:games:/usr/games:/sbin/nologin
        12    ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
        13    nobody:x:99:99:Nobody:/:/sbin/nologin
        14    systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
        15    dbus:x:81:81:System message bus:/:/sbin/nologin
        16    polkitd:x:999:998:User for polkitd:/:/sbin/nologin
        17    sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
        18    postfix:x:89:89::/var/spool/postfix:/sbin/nologin
        19    wang:x:1000:1000:wang:/home/wang:/bin/bash

    /pat/,#

    [root@centos72 ~]# cat    -n   /etc/passwd     
         1    root:x:0:0:root:/root:/bin/bash
         2    bin:x:1:1:bin:/bin:/sbin/nologin
         3    daemon:x:2:2:daemon:/sbin:/sbin/nologin
         4    adm:x:3:4:adm:/var/adm:/sbin/nologin
         5    lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
         6    sync:x:5:0:sync:/sbin:/bin/sync
         7    shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
         8    halt:x:7:0:halt:/sbin:/sbin/halt
         9    mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
        10    operator:x:11:0:operator:/root:/sbin/nologin
        11    games:x:12:100:games:/usr/games:/sbin/nologin
        12    ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
        13    nobody:x:99:99:Nobody:/:/sbin/nologin
        14    systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
        15    dbus:x:81:81:System message bus:/:/sbin/nologin
        16    polkitd:x:999:998:User for polkitd:/:/sbin/nologin
        17    sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
        18    postfix:x:89:89::/var/spool/postfix:/sbin/nologin
        19    wang:x:1000:1000:wang:/home/wang:/bin/bash
    [root@centos72 ~]# sed  -n   /^f/,+5p   /etc/passwd
    ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
    nobody:x:99:99:Nobody:/:/sbin/nologin
    systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
    dbus:x:81:81:System message bus:/:/sbin/nologin
    polkitd:x:999:998:User for polkitd:/:/sbin/nologin
    sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
    [root@centos72 ~]# sed  -n   /^f/,15p   /etc/passwd
    ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
    nobody:x:99:99:Nobody:/:/sbin/nologin
    systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
    dbus:x:81:81:System message bus:/:/sbin/nologin


    (4) ~:步进
    1~2 奇数行,表示从第1行开始打印,每2行打印一次
    2~2 偶数行,表示从第2行开始打印,每2行打印一次

    1)打印文件的奇数行

    [root@centos72 ~]# sed  -n   1~2p   /etc/passwd
    root:x:0:0:root:/root:/bin/bash
    daemon:x:2:2:daemon:/sbin:/sbin/nologin
    lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
    shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
    mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
    games:x:12:100:games:/usr/games:/sbin/nologin
    nobody:x:99:99:Nobody:/:/sbin/nologin
    dbus:x:81:81:System message bus:/:/sbin/nologin
    sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
    wang:x:1000:1000:wang:/home/wang:/bin/bash
    [root@centos72 ~]#  cat  /etc/passwd   -n  |   sed  -n   1~2p  
         1    root:x:0:0:root:/root:/bin/bash
         3    daemon:x:2:2:daemon:/sbin:/sbin/nologin
         5    lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
         7    shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
         9    mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
        11    games:x:12:100:games:/usr/games:/sbin/nologin
        13    nobody:x:99:99:Nobody:/:/sbin/nologin
        15    dbus:x:81:81:System message bus:/:/sbin/nologin
        17    sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
        19    wang:x:1000:1000:wang:/home/wang:/bin/bash

    2)打印文件的偶数行

    [root@centos72 ~]#  cat  /etc/passwd   -n  |  sed    -n    2~2p 
         2    bin:x:1:1:bin:/bin:/sbin/nologin
         4    adm:x:3:4:adm:/var/adm:/sbin/nologin
         6    sync:x:5:0:sync:/sbin:/bin/sync
         8    halt:x:7:0:halt:/sbin:/sbin/halt
        10    operator:x:11:0:operator:/root:/sbin/nologin
        12    ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
        14    systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
        16    polkitd:x:999:998:User for polkitd:/:/sbin/nologin
        18    postfix:x:89:89::/var/spool/postfix:/sbin/nologin

    四编辑命令的使用

    (一)d: 删除模式空间匹配的行,并立即启用下一轮循环


    d: 删除模式空间匹配的行,并立即启用下一轮循环,也就是读入新的一行

    (1)删除命令的第几行到第几行

    删除文件的第6行到第16行

    [root@centos72 ~]# cat  /etc/passwd   -n  |  sed    6,16d
         1    root:x:0:0:root:/root:/bin/bash
         2    bin:x:1:1:bin:/bin:/sbin/nologin
         3    daemon:x:2:2:daemon:/sbin:/sbin/nologin
         4    adm:x:3:4:adm:/var/adm:/sbin/nologin
         5    lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
        17    sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
        18    postfix:x:89:89::/var/spool/postfix:/sbin/nologin
        19    wang:x:1000:1000:wang:/home/wang:/bin/bash

    (2)利用删除命令打印奇数行,也就是把偶数行删除了

    法1:

    [root@centos72 ~]#  cat  /etc/passwd   -n  |  sed    -n    1~2p 
         1    root:x:0:0:root:/root:/bin/bash
         3    daemon:x:2:2:daemon:/sbin:/sbin/nologin
         5    lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
         7    shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
         9    mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
        11    games:x:12:100:games:/usr/games:/sbin/nologin
        13    nobody:x:99:99:Nobody:/:/sbin/nologin
        15    dbus:x:81:81:System message bus:/:/sbin/nologin
        17    sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
        19    wang:x:1000:1000:wang:/home/wang:/bin/bash
    

     法2:

    [root@centos72 ~]#  cat  /etc/passwd   -n  |  sed    -n    2~2d
    [root@centos72 ~]#  cat  /etc/passwd   -n  |  sed        2~2d
         1    root:x:0:0:root:/root:/bin/bash
         3    daemon:x:2:2:daemon:/sbin:/sbin/nologin
         5    lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
         7    shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
         9    mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
        11    games:x:12:100:games:/usr/games:/sbin/nologin
        13    nobody:x:99:99:Nobody:/:/sbin/nologin
        15    dbus:x:81:81:System message bus:/:/sbin/nologin
        17    sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
        19    wang:x:1000:1000:wang:/home/wang:/bin/bash

    (3)把标准输入指定行删除

    显示网络状态信息

    [root@centos72 ~]# ss  -tnl
    State       Recv-Q Send-Q                                 Local Address:Port                                                Peer Address:Port              
    LISTEN      0      128                                                *:22                                                             *:*                  
    LISTEN      0      100                                        127.0.0.1:25                                                             *:*                  
    LISTEN      0      128                                               :::22                                                            :::*                  
    LISTEN      0      100                                              ::1:25                                                            :::*

    删除网络状态信息的第1行

    命令可以不加引号

    [root@centos72 ~]# ss  -tnl   |  sed    1d
    LISTEN     0      128          *:22                       *:*                  
    LISTEN     0      100    127.0.0.1:25                       *:*                  
    LISTEN     0      128         :::22                      :::*                  
    LISTEN     0      100        ::1:25                      :::*     
    [root@centos72 ~]# ss  -tnl   |  sed    '1d'
    LISTEN     0      128          *:22                       *:*                  
    LISTEN     0      100    127.0.0.1:25                       *:*                  
    LISTEN     0      128         :::22                      :::*                  
    LISTEN     0      100        ::1:25                      :::*           

    打印以sd开头的内容

    /是进行模式匹配的,和/dev里面的斜线冲突,所以要对非模式匹配的斜线进行转义

    注意打印时最好都加上引号,否则会报错

    [root@centos72 ~]# df  |   sed  -n     /^/dev/sd/p
    sed: -e expression #1, char 5: extra characters after command
    [root@centos72 ~]# df  |   sed  -n     '/^/dev/sd/p'
    /dev/sda2       52403200 1119724  51283476   3% /
    /dev/sda3       20961280   32944  20928336   1% /app
    /dev/sda1        1038336  126596    911740  13% /boot

    如果不转义就会报错

    /pattern/:被此处模式所能够匹配到的每一行

    [root@centos72 ~]# df  |   sed  -n     '/^/dev/sd/p'
    sed: -e expression #1, char 5: extra characters after command

     不可以使用其他符合进行分隔

    [root@centos73 ~]#  df  |   sed  -n     '#^/dev/sd#p'
    [root@centos73 ~]#  df  |   sed  -n     '#^/dev/sd#p'

    (二)a []text:在指定行后面追加文本,支持使用 实现多行追加

    (1)在文件的第5行到第10行的每行的下一行添加&&&&&&&&&

    [root@centos72 ~]# cat  -n  /etc/passwd   |  sed    '5,10a&&&&&&&&&'
         1    root:x:0:0:root:/root:/bin/bash
         2    bin:x:1:1:bin:/bin:/sbin/nologin
         3    daemon:x:2:2:daemon:/sbin:/sbin/nologin
         4    adm:x:3:4:adm:/var/adm:/sbin/nologin
         5    lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
    &&&&&&&&&
         6    sync:x:5:0:sync:/sbin:/bin/sync
    &&&&&&&&&
         7    shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
    &&&&&&&&&
         8    halt:x:7:0:halt:/sbin:/sbin/halt
    &&&&&&&&&
         9    mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
    &&&&&&&&&
        10    operator:x:11:0:operator:/root:/sbin/nologin
    &&&&&&&&&
        11    games:x:12:100:games:/usr/games:/sbin/nologin
        12    ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
        13    nobody:x:99:99:Nobody:/:/sbin/nologin
        14    systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
        15    dbus:x:81:81:System message bus:/:/sbin/nologin
        16    polkitd:x:999:998:User for polkitd:/:/sbin/nologin
        17    sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
        18    postfix:x:89:89::/var/spool/postfix:/sbin/nologin
        19    wang:x:1000:1000:wang:/home/wang:/bin/bash

    注意 一定要加引号

    [root@centos65 ~]#  cat  -n  /etc/passwd   |  sed    5,10a&&&&&&&&&
    -bash: syntax error near unexpected token `&&'

    双引号也可以

    [root@centos65 ~]#  cat  -n  /etc/passwd   |  sed    "5,10a&&&&&&&&&"
         1    root:x:0:0:root:/root:/bin/bash
         2    bin:x:1:1:bin:/bin:/sbin/nologin
         3    daemon:x:2:2:daemon:/sbin:/sbin/nologin
         4    adm:x:3:4:adm:/var/adm:/sbin/nologin
         5    lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
    &&&&&&&&&
         6    sync:x:5:0:sync:/sbin:/bin/sync
    &&&&&&&&&
         7    shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
    &&&&&&&&&
         8    halt:x:7:0:halt:/sbin:/sbin/halt
    &&&&&&&&&
         9    mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
    &&&&&&&&&
        10    uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
    &&&&&&&&&
        11    operator:x:11:0:operator:/root:/sbin/nologin
        12    games:x:12:100:games:/usr/games:/sbin/nologin
        13    gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
        14    ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
        15    nobody:x:99:99:Nobody:/:/sbin/nologin
        16    vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin
        17    saslauth:x:499:76:Saslauthd user:/var/empty/saslauth:/sbin/nologin
        18    postfix:x:89:89::/var/spool/postfix:/sbin/nologin
        19    sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
        20    dbus:x:81:81:System message bus:/:/sbin/nologin
        21    ntp:x:38:38::/etc/ntp:/sbin/nologin
        22    abrt:x:173:173::/etc/abrt:/sbin/nologin
        23    haldaemon:x:68:68:HAL daemon:/:/sbin/nologin
        24    tcpdump:x:72:72::/:/sbin/nologin
        25    rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
        26    rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
        27    nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
        28    radvd:x:75:75:radvd user:/:/sbin/nologin
        29    qemu:x:107:107:qemu user:/:/sbin/nologin
        30    ident:x:98:98::/:/sbin/nologin

    原文件的内容没有真正被修改

    [root@centos72 ~]# cat  /etc/passwd  -n
         1    root:x:0:0:root:/root:/bin/bash
         2    bin:x:1:1:bin:/bin:/sbin/nologin
         3    daemon:x:2:2:daemon:/sbin:/sbin/nologin
         4    adm:x:3:4:adm:/var/adm:/sbin/nologin
         5    lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
         6    sync:x:5:0:sync:/sbin:/bin/sync
         7    shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
         8    halt:x:7:0:halt:/sbin:/sbin/halt
         9    mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
        10    operator:x:11:0:operator:/root:/sbin/nologin
        11    games:x:12:100:games:/usr/games:/sbin/nologin
        12    ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
        13    nobody:x:99:99:Nobody:/:/sbin/nologin
        14    systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
        15    dbus:x:81:81:System message bus:/:/sbin/nologin
        16    polkitd:x:999:998:User for polkitd:/:/sbin/nologin
        17    sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
        18    postfix:x:89:89::/var/spool/postfix:/sbin/nologin
        19    wang:x:1000:1000:wang:/home/wang:/bin/bash

    (2)命令和文件之间有没有空格显示的结果都一样的。

    [root@centos72 ~]# cat  -n  /etc/passwd   |  sed    '5,10a      &&&&&&&&&'
         1    root:x:0:0:root:/root:/bin/bash
         2    bin:x:1:1:bin:/bin:/sbin/nologin
         3    daemon:x:2:2:daemon:/sbin:/sbin/nologin
         4    adm:x:3:4:adm:/var/adm:/sbin/nologin
         5    lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
    &&&&&&&&&
         6    sync:x:5:0:sync:/sbin:/bin/sync
    &&&&&&&&&
         7    shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
    &&&&&&&&&
         8    halt:x:7:0:halt:/sbin:/sbin/halt
    &&&&&&&&&
         9    mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
    &&&&&&&&&
        10    operator:x:11:0:operator:/root:/sbin/nologin
    &&&&&&&&&
        11    games:x:12:100:games:/usr/games:/sbin/nologin
        12    ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
        13    nobody:x:99:99:Nobody:/:/sbin/nologin
        14    systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
        15    dbus:x:81:81:System message bus:/:/sbin/nologin
        16    polkitd:x:999:998:User for polkitd:/:/sbin/nologin
        17    sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
        18    postfix:x:89:89::/var/spool/postfix:/sbin/nologin
        19    wang:x:1000:1000:wang:/home/wang:/bin/bash
    [root@centos72 ~]# cat  -n  /etc/passwd   |  sed    '5,10        a&&&&&&&&&'
         1    root:x:0:0:root:/root:/bin/bash
         2    bin:x:1:1:bin:/bin:/sbin/nologin
         3    daemon:x:2:2:daemon:/sbin:/sbin/nologin
         4    adm:x:3:4:adm:/var/adm:/sbin/nologin
         5    lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
    &&&&&&&&&
         6    sync:x:5:0:sync:/sbin:/bin/sync
    &&&&&&&&&
         7    shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
    &&&&&&&&&
         8    halt:x:7:0:halt:/sbin:/sbin/halt
    &&&&&&&&&
         9    mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
    &&&&&&&&&
        10    operator:x:11:0:operator:/root:/sbin/nologin
    &&&&&&&&&
        11    games:x:12:100:games:/usr/games:/sbin/nologin
        12    ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
        13    nobody:x:99:99:Nobody:/:/sbin/nologin
        14    systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
        15    dbus:x:81:81:System message bus:/:/sbin/nologin
        16    polkitd:x:999:998:User for polkitd:/:/sbin/nologin
        17    sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
        18    postfix:x:89:89::/var/spool/postfix:/sbin/nologin
        19    wang:x:1000:1000:wang:/home/wang:/bin/bash

    (3)如果要想打印出空格的,那么就要a后面加上斜线,斜线就相当于边界符

    []表示此选项可有可无

    [root@centos72 ~]# cat  -n  /etc/passwd   |  sed    '5,10a           &&&&&&&&&'
         1    root:x:0:0:root:/root:/bin/bash
         2    bin:x:1:1:bin:/bin:/sbin/nologin
         3    daemon:x:2:2:daemon:/sbin:/sbin/nologin
         4    adm:x:3:4:adm:/var/adm:/sbin/nologin
         5    lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
               &&&&&&&&&
         6    sync:x:5:0:sync:/sbin:/bin/sync
               &&&&&&&&&
         7    shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
               &&&&&&&&&
         8    halt:x:7:0:halt:/sbin:/sbin/halt
               &&&&&&&&&
         9    mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
               &&&&&&&&&
        10    operator:x:11:0:operator:/root:/sbin/nologin
               &&&&&&&&&
        11    games:x:12:100:games:/usr/games:/sbin/nologin
        12    ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
        13    nobody:x:99:99:Nobody:/:/sbin/nologin
        14    systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
        15    dbus:x:81:81:System message bus:/:/sbin/nologin
        16    polkitd:x:999:998:User for polkitd:/:/sbin/nologin
        17    sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
        18    postfix:x:89:89::/var/spool/postfix:/sbin/nologin
        19    wang:x:1000:1000:wang:/home/wang:/bin/bash

    注意斜线要和后面的内容有空格,至少是一个空格

    [root@centos72 ~]# cat  -n  /etc/passwd   |  sed    '5,10a         &&&&&&&&&'
         1    root:x:0:0:root:/root:/bin/bash
         2    bin:x:1:1:bin:/bin:/sbin/nologin
         3    daemon:x:2:2:daemon:/sbin:/sbin/nologin
         4    adm:x:3:4:adm:/var/adm:/sbin/nologin
         5    lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
    &&&&&&&&&
         6    sync:x:5:0:sync:/sbin:/bin/sync
    &&&&&&&&&
         7    shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
    &&&&&&&&&
         8    halt:x:7:0:halt:/sbin:/sbin/halt
    &&&&&&&&&
         9    mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
    &&&&&&&&&
        10    operator:x:11:0:operator:/root:/sbin/nologin
    &&&&&&&&&
        11    games:x:12:100:games:/usr/games:/sbin/nologin
        12    ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
        13    nobody:x:99:99:Nobody:/:/sbin/nologin
        14    systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
        15    dbus:x:81:81:System message bus:/:/sbin/nologin
        16    polkitd:x:999:998:User for polkitd:/:/sbin/nologin
        17    sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
        18    postfix:x:89:89::/var/spool/postfix:/sbin/nologin
        19    wang:x:1000:1000:wang:/home/wang:/bin/bash
    [root@centos72 ~]# cat  -n  /etc/passwd   |  sed    '5,10a                                   &&&&&&&&&'
         1    root:x:0:0:root:/root:/bin/bash
         2    bin:x:1:1:bin:/bin:/sbin/nologin
         3    daemon:x:2:2:daemon:/sbin:/sbin/nologin
         4    adm:x:3:4:adm:/var/adm:/sbin/nologin
         5    lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
                            &&&&&&&&&
         6    sync:x:5:0:sync:/sbin:/bin/sync
                            &&&&&&&&&
         7    shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
                            &&&&&&&&&
         8    halt:x:7:0:halt:/sbin:/sbin/halt
                            &&&&&&&&&
         9    mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
                            &&&&&&&&&
        10    operator:x:11:0:operator:/root:/sbin/nologin
                            &&&&&&&&&
        11    games:x:12:100:games:/usr/games:/sbin/nologin
        12    ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
        13    nobody:x:99:99:Nobody:/:/sbin/nologin
        14    systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
        15    dbus:x:81:81:System message bus:/:/sbin/nologin
        16    polkitd:x:999:998:User for polkitd:/:/sbin/nologin
        17    sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
        18    postfix:x:89:89::/var/spool/postfix:/sbin/nologin
        19    wang:x:1000:1000:wang:/home/wang:/bin/bash

     实例:

    在第8行之后追加

    现在只是显示,没有真正追加到文件里面

    因为模式匹配是单引号,所以里面的追加内容只能使用双引号,否则会引起冲突的

    [root@centos72 ~]# sed  '8a   alias  cdne="cd   /etc/sysconfig/network-scripts/"'    ~/.bashrc 
    # .bashrc
    
    # User specific aliases and functions
    
    alias rm='rm -i'
    alias cp='cp -i'
    alias mv='mv -i'
    
    alias  cdne="cd   /etc/sysconfig/network-scripts/"
    # Source global definitions
    if [ -f /etc/bashrc ]; then
        . /etc/bashrc
    fi

    原文件内容保持不变

    第8行是空行

    [root@centos72 ~]# cat  -n   ~/.bashrc 
         1    # .bashrc
         2    
         3    # User specific aliases and functions
         4    
         5    alias rm='rm -i'
         6    alias cp='cp -i'
         7    alias mv='mv -i'
         8    
         9    # Source global definitions
        10    if [ -f /etc/bashrc ]; then
        11        . /etc/bashrc
        12    fi

    (三)-i.bak: 备份文件并原处编辑

    真正追加到文件里面,要谨慎使用,确定没有问题再确定追加,凡事三思而后行。

    -i.bak: 备份文件并原处编辑,也就是先对原来的文件进行备份再修改,这样会更好。

    我们在对某个服务的配置文件进行修改时也要养成这种好习惯,先备份再修改,运维工作最重要的是备份。

    如果是追加到文件的最后一行,使用echo重定向就可以了

    [root@centos72 ~]# sed   -i.bak    '8a   alias  cdne="cd   /etc/sysconfig/network-scripts/"'    ~/.bashrc
    [root@centos72 ~]# cat  -n   ~/.bashrc   
         1    # .bashrc
         2    
         3    # User specific aliases and functions
         4    
         5    alias rm='rm -i'
         6    alias cp='cp -i'
         7    alias mv='mv -i'
         8    
         9    alias  cdne="cd   /etc/sysconfig/network-scripts/"
        10    # Source global definitions
        11    if [ -f /etc/bashrc ]; then
        12        . /etc/bashrc
        13    fi
    [root@centos72 ~]# ls  -a  ~/
    .  ..  anaconda-ks.cfg  .bash_history  .bash_logout  .bash_profile  .bashrc  .bashrc.bak  .cshrc  .tcshrc  .viminfo
    #生成了备份文件.bashrc.bak

    (四)i []text:在匹配的行前面插入文本

    和a命令作用相似,结果相反。

    使用到正则表达式,^表示行首

    /^# Source/模式匹配,表示以# Source开头的行

    [root@centos72 ~]# sed   -i.bak    '/^# Source/i$$$$$$$'    ~/.bashrc
    [root@centos72 ~]# cat  -n   ~/.bashrc   
         1    # .bashrc
         2    
         3    # User specific aliases and functions
         4    
         5    alias rm='rm -i'
         6    alias cp='cp -i'
         7    alias mv='mv -i'
         8    
         9    alias  cdne="cd   /etc/sysconfig/network-scripts/"
        10    $$$$$$$
        11    # Source global definitions
        12    if [ -f /etc/bashrc ]; then
        13        . /etc/bashrc
        14    fi

     和a一样,如果和后面的文本没有空格那么和没加是一样的结果

    [root@centos72 ~]# sed      '/^# Source/i**********'    ~/.bashrc
    # .bashrc
    
    # User specific aliases and functions
    
    alias rm='rm -i'
    alias cp='cp -i'
    alias mv='mv -i'
    
    alias  cdne="cd   /etc/sysconfig/network-scripts/"
    $$$$$$$
    **********
    # Source global definitions
    if [ -f /etc/bashrc ]; then
        . /etc/bashrc
    fi
    [root@centos72 ~]# sed      '/^# Source/i       **********'    ~/.bashrc
    # .bashrc
    
    # User specific aliases and functions
    
    alias rm='rm -i'
    alias cp='cp -i'
    alias mv='mv -i'
    
    alias  cdne="cd   /etc/sysconfig/network-scripts/"
    $$$$$$$
           **********
    # Source global definitions
    if [ -f /etc/bashrc ]; then
        . /etc/bashrc
    fi
    [root@centos65 ~]# sed      '/^# Source/i     **********'    ~/.bashrc
    # .bashrc
    
    # User specific aliases and functions
    
    alias rm='rm -i'
    alias cp='cp -i'
    alias mv='mv -i'
    
         **********
    # Source global definitions
    if [ -f /etc/bashrc ]; then
        . /etc/bashrc
    fi

    对比:

     a是在行后追加

    [root@centos72 ~]# sed      '/^# Source/a**********'    ~/.bashrc
    # .bashrc
    
    # User specific aliases and functions
    
    alias rm='rm -i'
    alias cp='cp -i'
    alias mv='mv -i'
    
    alias  cdne="cd   /etc/sysconfig/network-scripts/"
    $$$$$$$
    # Source global definitions
    **********
    if [ -f /etc/bashrc ]; then
        . /etc/bashrc
    fi
    [root@centos72 ~]# sed      '/^# Source/a**********'    ~/.bashrc
    # .bashrc
    
    # User specific aliases and functions
    
    alias rm='rm -i'
    alias cp='cp -i'
    alias mv='mv -i'
    
    alias  cdne="cd   /etc/sysconfig/network-scripts/"
    $$$$$$$
    # Source global definitions
    **********
    if [ -f /etc/bashrc ]; then
        . /etc/bashrc
    fi
    [root@centos72 ~]# sed      '/^# Source/a       **********'    ~/.bashrc
    # .bashrc
    
    # User specific aliases and functions
    
    alias rm='rm -i'
    alias cp='cp -i'
    alias mv='mv -i'
    
    alias  cdne="cd   /etc/sysconfig/network-scripts/"
    $$$$$$$
    # Source global definitions
         **********
    if [ -f /etc/bashrc ]; then
        . /etc/bashrc
    fi

    实现多行追加

    这样就可以一次性加好几条别名

    [root@centos72 ~]# cat   ~/.bashrc
    # .bashrc
    
    # User specific aliases and functions
    
    alias rm='rm -i'
    alias cp='cp -i'
    alias mv='mv -i'
    
    alias  cdne="cd   /etc/sysconfig/network-scripts/"
    $$$$$$$
    # Source global definitions
    if [ -f /etc/bashrc ]; then
        . /etc/bashrc
    fi
    [root@centos72 ~]# sed      '/^# Source/ixxxxxxxxxxxx
    yyyyyyyyyy
    zzzzzzzzzzzzz'    ~/.bashrc
    # .bashrc
    
    # User specific aliases and functions
    
    alias rm='rm -i'
    alias cp='cp -i'
    alias mv='mv -i'
    
    alias  cdne="cd   /etc/sysconfig/network-scripts/"
    $$$$$$$
    xxxxxxxxxxxx
    yyyyyyyyyy
    zzzzzzzzzzzzz
    # Source global definitions
    if [ -f /etc/bashrc ]; then
        . /etc/bashrc
    fi

    注意此时的家目录是root

    所以~/.bashrc和/root/.bashrc是等价的

    [root@centos72 ~]# cat  ~/.bashrc
    # .bashrc
    
    # User specific aliases and functions
    
    alias rm='rm -i'
    alias cp='cp -i'
    alias mv='mv -i'
    alias ka='systemctl   restart  keepalived.service'
    
    
    
    # Source global definitions
    if [ -f /etc/bashrc ]; then
        . /etc/bashrc
    fi
    [root@centos72 ~]# pwd
    /root
    [root@centos72 ~]# cat  /root/.bashrc
    # .bashrc
    
    # User specific aliases and functions
    
    alias rm='rm -i'
    alias cp='cp -i'
    alias mv='mv -i'
    alias ka='systemctl   restart  keepalived.service'
    
    
    
    # Source global definitions
    if [ -f /etc/bashrc ]; then
        . /etc/bashrc
    fi
    [root@centos65 ~]# diff  /root/.bashr^C ~/.bashrc 
    [root@centos65 ~]# who
    root     pts/0        2019-01-25 05:32 (192.168.137.1)
    [root@centos65 ~]# whoami
    root
    

    (五)c []text:替换行为单行或多行文本

    [root@centos72 ~]# cat  -n   ~/.bashrc   
         1    # .bashrc
         2    
         3    # User specific aliases and functions
         4    
         5    alias rm='rm -i'
         6    alias cp='cp -i'
         7    alias mv='mv -i'
         8    
         9    alias  cdne="cd   /etc/sysconfig/network-scripts/"
        10    $$$$$$$
        11    # Source global definitions
        12    if [ -f /etc/bashrc ]; then
        13        . /etc/bashrc
        14    fi
    [root@centos72 ~]# sed      '/^# Source/c      **********'    ~/.bashrc
    # .bashrc
    
    # User specific aliases and functions
    
    alias rm='rm -i'
    alias cp='cp -i'
    alias mv='mv -i'
    
    alias  cdne="cd   /etc/sysconfig/network-scripts/"
    $$$$$$$
         **********
    if [ -f /etc/bashrc ]; then
        . /etc/bashrc
    fi
    [root@centos72 ~]# sed      '/^# Source/c    **********'    ~/.bashrc
    # .bashrc
    
    # User specific aliases and functions
    
    alias rm='rm -i'
    alias cp='cp -i'
    alias mv='mv -i'
    
    alias  cdne="cd   /etc/sysconfig/network-scripts/"
    $$$$$$$
    **********
    if [ -f /etc/bashrc ]; then
        . /etc/bashrc
    fi
    [root@centos65 ~]# cat  ~/.bashrc
    # .bashrc
    
    # User specific aliases and functions
    
    alias rm='rm -i'
    alias cp='cp -i'
    alias mv='mv -i'
    
    # Source global definitions
    if [ -f /etc/bashrc ]; then
        . /etc/bashrc
    fi
    [root@centos65 ~]# sed      '/^# Source/c    **********'    ~/.bashrc
    # .bashrc
    
    # User specific aliases and functions
    
    alias rm='rm -i'
    alias cp='cp -i'
    alias mv='mv -i'
    
    **********
    if [ -f /etc/bashrc ]; then
        . /etc/bashrc
    fi
    [root@centos65 ~]# cat  ~/.bashrc
    # .bashrc
    
    # User specific aliases and functions
    
    alias rm='rm -i'
    alias cp='cp -i'
    alias mv='mv -i'
    
    # Source global definitions
    if [ -f /etc/bashrc ]; then
        . /etc/bashrc
    fi

     实例:

    (1)实现一行替换

    替换的很实用,特别是在修改配置文件的时候

    /pattern/:被此处模式所能够匹配到的每一行

    # This file controls the state of SELinux on the system.
    # SELINUX= can take one of these three values:
    #     enforcing - SELinux security policy is enforced.
    #     permissive - SELinux prints warnings instead of enforcing.
    #     disabled - No SELinux policy is loaded.
    SELINUX=enforcing
    # SELINUXTYPE= can take one of three two values:
    #     targeted - Targeted processes are protected,
    #     minimum - Modification of targeted policy. Only selected processes are protected. 
    #     mls - Multi Level Security protection.
    SELINUXTYPE=targeted 
    
    
    [root@centos72 ~]# sed   -i.bak   '/^SELINUX=/c     SELINUX=disabled'     /etc/selinux/config 
    [root@centos72 ~]# cat  /etc/selinux/config 
    
    # This file controls the state of SELinux on the system.
    # SELINUX= can take one of these three values:
    #     enforcing - SELinux security policy is enforced.
    #     permissive - SELinux prints warnings instead of enforcing.
    #     disabled - No SELinux policy is loaded.
    SELINUX=disabled
    # SELINUXTYPE= can take one of three two values:
    #     targeted - Targeted processes are protected,
    #     minimum - Modification of targeted policy. Only selected processes are protected. 
    #     mls - Multi Level Security protection.
    SELINUXTYPE=targeted 

    /^SELINUX=/表示匹配以SELINUX=开头的行

    [root@centos65 ~]# cat  /etc/selinux/config
    
    # This file controls the state of SELinux on the system.
    # SELINUX= can take one of these three values:
    #     enforcing - SELinux security policy is enforced.
    #     permissive - SELinux prints warnings instead of enforcing.
    #     disabled - No SELinux policy is loaded.
    SELINUX=enforcing
    # SELINUXTYPE= can take one of these two values:
    #     targeted - Targeted processes are protected,
    #     mls - Multi Level Security protection.
    SELINUXTYPE=targeted 
    
    
    [root@centos65 ~]# #sed     '/^SELINUX=/c     SELINUX=disabled'     /etc/selinux/config 
    [root@centos65 ~]# sed     '/^SELINUX=/c     SELINUX=disabled'     /etc/selinux/config 
    
    # This file controls the state of SELinux on the system.
    # SELINUX= can take one of these three values:
    #     enforcing - SELinux security policy is enforced.
    #     permissive - SELinux prints warnings instead of enforcing.
    #     disabled - No SELinux policy is loaded.
    SELINUX=disabled
    # SELINUXTYPE= can take one of these two values:
    #     targeted - Targeted processes are protected,
    #     mls - Multi Level Security protection.
    SELINUXTYPE=targeted 

    (六)w    /path/somefile: 保存模式匹配的行至指定文件

    w    /path/somefile: 保存模式匹配的行至指定文件,要加上单引号才可以

    [root@centos72 ~]# sed  /^alias/w    /app/alias.txt
    sed: couldn't open file : No such file or directory
    [root@centos72 ~]# sed  /^alias/w    /app/alias.txt     ~/.bashrc
    sed: couldn't open file : No such file or directory
    [root@centos72 ~]# sed  '/^alias/w    /app/alias.txt'     ~/.bashrc
    # .bashrc
    
    # User specific aliases and functions
    
    alias rm='rm -i'
    alias cp='cp -i'
    alias mv='mv -i'
    
    alias  cdne="cd   /etc/sysconfig/network-scripts/"
    $$$$$$$
    # Source global definitions
    if [ -f /etc/bashrc ]; then
        . /etc/bashrc
    fi

    执行结果,下面是自动创建的文件

    [root@centos72 ~]# cat  /app/alias.txt 
    alias rm='rm -i'
    alias cp='cp -i'
    alias mv='mv -i'
    alias ka='systemctl   restart  keepalived.service'

    也就是把最后的文件匹配到的内容复制到中间指定的文件

    [root@centos73 ~]# sed  '/^alias/w    /app/alias.txt'     ~/.bashrc
    # .bashrc
    
    # User specific aliases and functions
    
    alias rm='rm -i'
    alias cp='cp -i'
    alias mv='mv -i'
    
    # Source global definitions
    if [ -f /etc/bashrc ]; then
        . /etc/bashrc
    fi
    [root@centos73 ~]# cat  /app/alias.txt' 
    > ^C
    [root@centos73 ~]# cat  /app/alias.txt
    alias rm='rm -i'
    alias cp='cp -i'
    alias mv='mv -i'
    [root@centos73 ~]# ll /app/alias.txt 
    -rw-r--r--. 1 root root 51 Jun 20 14:07 /app/alias.txt

    使用双引号也可以

    和变量一样要加引号

    [root@centos72 ~]# sed  "/^alias/w    /app/as.txt"     ~/.bashrc
    # .bashrc
    
    # User specific aliases and functions
    
    alias rm='rm -i'
    alias cp='cp -i'
    alias mv='mv -i'
    alias ka='systemctl   restart  keepalived.service'
    
    
    
    # Source global definitions
    if [ -f /etc/bashrc ]; then
        . /etc/bashrc
    fi
    [root@centos72 ~]# cat /app/a
    alias.txt  as.txt     
    [root@centos72 ~]# cat /app/as.txt 
    alias rm='rm -i'
    alias cp='cp -i'
    alias mv='mv -i'
    alias ka='systemctl   restart  keepalived.service'

    (七)r  /path/somefile:读取指定文件的文本至模式空间中匹配到的行后

    alias开头的行后面追加文件的内容

    [root@centos72 ~]# sed     '/^alias/r  /etc/centos-release'     ~/.bashrc
    # .bashrc
    
    # User specific aliases and functions
    
    alias rm='rm -i'
    CentOS Linux release 7.5.1804 (Core) 
    alias cp='cp -i'
    CentOS Linux release 7.5.1804 (Core) 
    alias mv='mv -i'
    CentOS Linux release 7.5.1804 (Core) 
    
    alias  cdne="cd   /etc/sysconfig/network-scripts/"
    CentOS Linux release 7.5.1804 (Core) 
    $$$$$$$
    # Source global definitions
    if [ -f /etc/bashrc ]; then
        . /etc/bashrc
    fi

    =: 为模式空间中的行打印行号

    [root@centos72 ~]# sed     '/^alias/='     ~/.bashrc
    # .bashrc
    
    # User specific aliases and functions
    
    5
    alias rm='rm -i'
    6
    alias cp='cp -i'
    7
    alias mv='mv -i'
    
    9
    alias  cdne="cd   /etc/sysconfig/network-scripts/"
    $$$$$$$
    # Source global definitions
    if [ -f /etc/bashrc ]; then
        . /etc/bashrc
    fi
    [root@centos72 ~]# cat  -n  .bashrc
         1    # .bashrc
         2    
         3    # User specific aliases and functions
         4    
         5    alias rm='rm -i'
         6    alias cp='cp -i'
         7    alias mv='mv -i'
         8    
         9    alias  cdne="cd   /etc/sysconfig/network-scripts/"
        10    $$$$$$$
        11    # Source global definitions
        12    if [ -f /etc/bashrc ]; then
        13        . /etc/bashrc
        14    fi

    (八)!:模式空间中匹配行取反处理

    在条件测试里面!也表示取反

    [root@centos72 ~]# sed     '/^alias/p'     ~/.bashrc
    # .bashrc
    
    # User specific aliases and functions
    
    alias rm='rm -i'
    alias rm='rm -i'
    alias cp='cp -i'
    alias cp='cp -i'
    alias mv='mv -i'
    alias mv='mv -i'
    
    alias  cdne="cd   /etc/sysconfig/network-scripts/"
    alias  cdne="cd   /etc/sysconfig/network-scripts/"
    $$$$$$$
    # Source global definitions
    if [ -f /etc/bashrc ]; then
        . /etc/bashrc
    fi

    注意!只能加在命令的前面

    [root@centos72 ~]# sed     '/^alias/p!'     ~/.bashrc
    sed: -e expression #1, char 10: extra characters after command
    [root@centos72 ~]# sed     '/^alias/!p'     ~/.bashrc
    # .bashrc
    # .bashrc
    
    
    # User specific aliases and functions
    # User specific aliases and functions
    
    
    alias rm='rm -i'
    alias cp='cp -i'
    alias mv='mv -i'
    
    
    alias  cdne="cd   /etc/sysconfig/network-scripts/"
    $$$$$$$
    $$$$$$$
    # Source global definitions
    # Source global definitions
    if [ -f /etc/bashrc ]; then
    if [ -f /etc/bashrc ]; then
        . /etc/bashrc
        . /etc/bashrc
    fi
    fi

     取反之后就不打印alias开头的行了

    [root@centos72 ~]# sed     -n    '/^alias/p'     ~/.bashrc
    alias rm='rm -i'
    alias cp='cp -i'
    alias mv='mv -i'
    alias  cdne="cd   /etc/sysconfig/network-scripts/"
    [root@centos72 ~]# sed     -n    '/^alias/!p'     ~/.bashrc
    # .bashrc
    
    # User specific aliases and functions
    
    
    $$$$$$$
    # Source global definitions
    if [ -f /etc/bashrc ]; then
        . /etc/bashrc
    fi

    显示文件中以bash结尾的行,涉及到正则表达式$

    [root@centos72 ~]# sed  -n    '/bash$/p'  /etc/passwd
    root:x:0:0:root:/root:/bin/bash
    wang:x:1000:1000:wang:/home/wang:/bin/bash
    [root@centos72 ~]# cat  /etc/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
    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
    games:x:12:100:games:/usr/games:/sbin/nologin
    ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
    nobody:x:99:99:Nobody:/:/sbin/nologin
    systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
    dbus:x:81:81:System message bus:/:/sbin/nologin
    polkitd:x:999:998:User for polkitd:/:/sbin/nologin
    sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
    postfix:x:89:89::/var/spool/postfix:/sbin/nologin
    wang:x:1000:1000:wang:/home/wang:/bin/bash

    打印除bash结尾的行

    [root@centos72 ~]# sed  -n    '/bash$/!p'  /etc/passwd
    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
    games:x:12:100:games:/usr/games:/sbin/nologin
    ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
    nobody:x:99:99:Nobody:/:/sbin/nologin
    systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
    dbus:x:81:81:System message bus:/:/sbin/nologin
    polkitd:x:999:998:User for polkitd:/:/sbin/nologin
    sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
    postfix:x:89:89::/var/spool/postfix:/sbin/nologin


    作者:wang618
    出处:https://www.cnblogs.com/wang618/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利。

  • 相关阅读:
    接口隔离原则
    单一职责原则
    设计模式
    typora的快捷键构建
    typora的使用
    MapReuduce的一些情况
    Django之创建超级用户
    Tensorflow在python3.7版本的运行
    开启3个线程,这3个线程的ID分别为A、B、C,每个线程将自己的ID在屏幕上,要求输出结果必须按ABC的顺序显示:ABCABC
    VM10 win7 虚拟机+VS各种版本的快照...
  • 原文地址:https://www.cnblogs.com/wang618/p/11058536.html
Copyright © 2020-2023  润新知