sed命令

sed命令

sed是一种流编辑器,用于对文本逐行读取逐行处理,处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(patternspace),接着用sed命令处理缓冲区中的内容,处理完成后,把处理后结果输出到标准输出。接着处理下一行,这样不断重复,直到文件末尾。

命令语法

sed [OPTION]... {script-only-if-no-other-script} [input-file]...
    script:
        地址定界编辑命令
    常用选项:
        -n 不输出模式空间中内容至屏幕   
            [root@xulinhai tmp]# sed -n '1,3p' fstab 
            # /etc/fstab`
            # Created by anaconda on Wed Mar 22 23:57:53 2017
            #

        -e script, --expression=script 多点编辑
            [root@xulinhai tmp]# sed -n -e '1,3p' -e '5,6p' fstab 
            # /etc/fstab
            # Created by anaconda on Wed Mar 22 23:57:53 2017
            #
            # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
            #

        -f /PATH/TO/SED_SCRIPT_FILE
            把sed要执行的命令写到文件中,通过-f选项把文件中的命令传给sed
        -r, --regexp-extended 支持使用扩展正则表达式
        -i 直接编辑源文件

    地址定界
        1,空地址:对全文进行处理        
        2,单地址
            #:对指定行进行操作
                [root@xulinhai tmp]# sed -n '2p' fstab 
                # Created by anaconda on Wed Mar 22 23:57:53 2017
            /pattern/:被模式匹配到的每一行
                [root@xulinhai tmp]# sed -n '/UUID/p' fstab 
                UUID=b13a9a42-536e-4e57-aed0-24af0c7d450e /boot                   ext4    defaults        1 2
        3,地址范围
            #,# :起始行到结束行
                [root@xulinhai tmp]# sed -n '3,5p' fstab 
                #
                # 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                

            #,+n 起始行#往后加n行
                [root@xulinhai tmp]# sed -n '3,+5p' fstab 
                #
                # 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
                #
                /dev/mapper/vg_root-lv_root /                       ext4    defaults        1 1
                UUID=b13a9a42-536e-4e57-aed0-24af0c7d450e /boot                   ext4    defaults        1 2

            #,/pattern/ 从起始行#开始到pattern匹配到的行
                [root@xulinhai tmp]# sed -n '1,/UUID/p' fstab 
                # /etc/fstab
                # Created by anaconda on Wed Mar 22 23:57:53 2017
                #
                # 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
                #
                /dev/mapper/vg_root-lv_root /                       ext4    defaults        1 1
                UUID=b13a9a42-536e-4e57-aed0-24af0c7d450e /boot                   ext4    defaults        1 2

            /pattern1/,/pattern2/ 从pattern1匹配到的行到pattern2匹配到的行
                [root@xulinhai tmp]# sed -n '/anaconda/,/UUID/p' fstab 
                # Created by anaconda on Wed Mar 22 23:57:53 2017
                #
                # 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
                #
                /dev/mapper/vg_root-lv_root /                       ext4    defaults        1 1
                UUID=b13a9a42-536e-4e57-aed0-24af0c7d450e /boot                   ext4    defaults        1 2    


        4,步进:~
            1~2 所有奇数行
                [root@xulinhai tmp]# sed -n '1~2p' fstab 
                # /etc/fstab
                #
                # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
                /dev/mapper/vg_root-lv_root /                       ext4    defaults        1 1
                /dev/mapper/vg_root-lv_swap swap                    swap    defaults        0 0
                devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
                proc                    /proc                   proc    defaults        0 0
                /dev/vg_app/lv_template    /template        ext4    defaults    0 0 
            2~2 所有偶数行
                [root@xulinhai tmp]# sed -n '2~2p' fstab 
                # Created by anaconda on Wed Mar 22 23:57:53 2017
                # Accessible filesystems, by reference, are maintained under '/dev/disk'
                #
                UUID=b13a9a42-536e-4e57-aed0-24af0c7d450e /boot                   ext4    defaults        1 2
                tmpfs                   /dev/shm                tmpfs   defaults        0 0
                sysfs                   /sys                    sysfs   defaults        0 0
                #10.240.253.21:/hpetl    /hpetl            nfs    defaults    0 0    

    编辑命令:
        d 删除
            sed '1,5d' /tmp/fstab
            sed '/^UUID/d' /etc/fstab 
            sed '1~2d' /etc/fstab 删除奇数行,显示偶数行        

        p 打印
            sed '1~2p' /etc/fstab 奇数行显示两遍
            sed -n '1~2p' /etc/fstab 

        a\text 在匹配到的行的下方追加文本‘text’ ,支持使用\n多行插入.        
            [root@xulinhai tmp]# sed  '/UUID/a\this is a new line.\nthis is another line.' fstab 
            # /etc/fstab
            # Created by anaconda on Wed Mar 22 23:57:53 2017
            #
            # 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
            #
            /dev/mapper/vg_root-lv_root /                       ext4    defaults        1 1
            UUID=b13a9a42-536e-4e57-aed0-24af0c7d450e /boot                   ext4    defaults        1 2
            this is a new line.
            this is another line.
            /dev/mapper/vg_root-lv_swap swap                    swap    defaults        0 0
            tmpfs                   /dev/shm                tmpfs   defaults        0 0
            devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
            sysfs                   /sys                    sysfs   defaults        0 0
            proc                    /proc                   proc    defaults        0 0
            #10.240.253.21:/hpetl    /hpetl            nfs    defaults    0 0
            /dev/vg_app/lv_template    /template        ext4    defaults    0 0 

        i\text 在行的上方追加文本‘text’支持使用\n多行插入
            [root@xulinhai tmp]# sed  '/UUID/i\this is a new line.\nthis is another line.' fstab 
            # /etc/fstab
            # Created by anaconda on Wed Mar 22 23:57:53 2017
            #
            # 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
            #
            /dev/mapper/vg_root-lv_root /                       ext4    defaults        1 1
            this is a new line.
            this is another line.
            UUID=b13a9a42-536e-4e57-aed0-24af0c7d450e /boot                   ext4    defaults        1 2
            /dev/mapper/vg_root-lv_swap swap                    swap    defaults        0 0
            tmpfs                   /dev/shm                tmpfs   defaults        0 0
            devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
            sysfs                   /sys                    sysfs   defaults        0 0
            proc                    /proc                   proc    defaults        0 0
            #10.240.253.21:/hpetl    /hpetl            nfs    defaults    0 0
            /dev/vg_app/lv_template    /template        ext4    defaults    0 0 

        c\text 把匹配到的行整行替换为text    
            [root@xulinhai tmp]# sed  '2c\this is a new line.' fstab 
            # /etc/fstab
            this is a new line.
            #
            # 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
            #
            /dev/mapper/vg_root-lv_root /                       ext4    defaults        1 1
            UUID=b13a9a42-536e-4e57-aed0-24af0c7d450e /boot                   ext4    defaults        1 2
            /dev/mapper/vg_root-lv_swap swap                    swap    defaults        0 0
            tmpfs                   /dev/shm                tmpfs   defaults        0 0
            devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
            sysfs                   /sys                    sysfs   defaults        0 0
            proc                    /proc                   proc    defaults        0 0
            #10.240.253.21:/hpetl    /hpetl            nfs    defaults    0 0
            /dev/vg_app/lv_template    /template        ext4    defaults    0 0 

        w/path/to/somefile 把匹配到的行写入到指定文件中
            [root@xulinhai tmp]# sed  '/^[^#]/w/tmp/newfstab' fstab
            [root@xulinhai tmp]# cat newfstab 
            /dev/mapper/vg_root-lv_root /                       ext4    defaults        1 1
            UUID=b13a9a42-536e-4e57-aed0-24af0c7d450e /boot                   ext4    defaults        1 2
            /dev/mapper/vg_root-lv_swap swap                    swap    defaults        0 0
            tmpfs                   /dev/shm                tmpfs   defaults        0 0
            devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
            sysfs                   /sys                    sysfs   defaults        0 0
            proc                    /proc                   proc    defaults        0 0
            /dev/vg_app/lv_template    /template        ext4    defaults    0 0 

        r/path/from/somefile 读取指定文件中内容至当前文件被模式匹配到的下一行        
            [root@xulinhai tmp]# cat newfstab 
            this is a new line 
            [root@xulinhai tmp]# sed '/proc/r/tmp/newfstab' fstab 
            # /etc/fstab
            # Created by anaconda on Wed Mar 22 23:57:53 2017
            #
            # 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
            #
            /dev/mapper/vg_root-lv_root /                       ext4    defaults        1 1
            UUID=b13a9a42-536e-4e57-aed0-24af0c7d450e /boot                   ext4    defaults        1 2
            /dev/mapper/vg_root-lv_swap swap                    swap    defaults        0 0
            tmpfs                   /dev/shm                tmpfs   defaults        0 0
            devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
            sysfs                   /sys                    sysfs   defaults        0 0
            proc                    /proc                   proc    defaults        0 0
            this is a new line 
            #10.240.253.21:/hpetl    /hpetl            nfs    defaults    0 0
            /dev/vg_app/lv_template    /template        ext4    defaults    0 0

        = 为模式匹配到的行打印行号
            [root@xulinhai tmp]# sed '/^\//=' fstab #为斜线开头的行打印行号
            # /etc/fstab
            # Created by anaconda on Wed Mar 22 23:57:53 2017
            #
            # 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
            #
            7
            /dev/mapper/vg_root-lv_root /                       ext4    defaults        1 1
            UUID=b13a9a42-536e-4e57-aed0-24af0c7d450e /boot                   ext4    defaults        1 2
            9
            /dev/mapper/vg_root-lv_swap swap                    swap    defaults        0 0
            tmpfs                   /dev/shm                tmpfs   defaults        0 0
            devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
            sysfs                   /sys                    sysfs   defaults        0 0
            proc                    /proc                   proc    defaults        0 0
            #10.240.253.21:/hpetl    /hpetl            nfs    defaults    0 0
            15
            /dev/vg_app/lv_template    /template        ext4    defaults    0 0 

        !条件取反
            [root@xulinhai tmp]# sed -n '/^#/!p' fstab #打印非#开头的行
            /dev/mapper/vg_root-lv_root /                       ext4    defaults        1 1
            UUID=b13a9a42-536e-4e57-aed0-24af0c7d450e /boot                   ext4    defaults        1 2
            /dev/mapper/vg_root-lv_swap swap                    swap    defaults        0 0
            tmpfs                   /dev/shm                tmpfs   defaults        0 0
            devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
            sysfs                   /sys                    sysfs   defaults        0 0
            proc                    /proc                   proc    defaults        0 0
            /dev/vg_app/lv_template    /template        ext4    defaults    0 0 
            You have mail in /var/spool/mail/root

        s/// 查找替换 其分隔符可自行指定 如:s@@@, s###
            g 全局替换
                [root@xulinhai tmp]# sed 's/defaults/DEFAULTS/g' fstab 
                # /etc/fstab
                # Created by anaconda on Wed Mar 22 23:57:53 2017
                #
                # 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
                #
                /dev/mapper/vg_root-lv_root /                       ext4    DEFAULTS        1 1
                UUID=b13a9a42-536e-4e57-aed0-24af0c7d450e /boot                   ext4    DEFAULTS        1 2
                /dev/mapper/vg_root-lv_swap swap                    swap    DEFAULTS        0 0
                tmpfs                   /dev/shm                tmpfs   DEFAULTS        0 0
                devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
                sysfs                   /sys                    sysfs   DEFAULTS        0 0
                proc                    /proc                   proc    DEFAULTS        0 0
                #10.240.253.21:/hpetl    /hpetl            nfs    DEFAULTS    0 0
                /dev/vg_app/lv_template    /template        ext4    DEFAULTS    0 0 

            w/path/to/somefile 将替换成功的结果保存至指定文件                
                sed 's/defaults/DEFAULTS/gw/tmp/defaults' fstab #替换defaults为DEFAULTS    并保存至/tmp/defaults文件中
                [root@xulinhai tmp]# cat defaults 
                /dev/mapper/vg_root-lv_root /                       ext4    DEFAULTS        1 1
                UUID=b13a9a42-536e-4e57-aed0-24af0c7d450e /boot                   ext4    DEFAULTS        1 2
                /dev/mapper/vg_root-lv_swap swap                    swap    DEFAULTS        0 0
                tmpfs                   /dev/shm                tmpfs   DEFAULTS        0 0
                sysfs                   /sys                    sysfs   DEFAULTS        0 0
                proc                    /proc                   proc    DEFAULTS        0 0
                #10.240.253.21:/hpetl    /hpetl            nfs    DEFAULTS    0 0
                /dev/vg_app/lv_template    /template        ext4    DEFAULTS    0 0 

                [root@xulinhai tmp]# sed -i 's/defaults/DEFAULTS/g' fstab  #直接替换源文件中的defaults为DEFAULTS    
                [root@xulinhai tmp]# cat fstab 
                # /etc/fstab
                # Created by anaconda on Wed Mar 22 23:57:53 2017
                #
                # 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
                #
                /dev/mapper/vg_root-lv_root /                       ext4    DEFAULTS        1 1
                UUID=b13a9a42-536e-4e57-aed0-24af0c7d450e /boot                   ext4    DEFAULTS        1 2
                /dev/mapper/vg_root-lv_swap swap                    swap    DEFAULTS        0 0
                tmpfs                   /dev/shm                tmpfs   DEFAULTS        0 0
                devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
                sysfs                   /sys                    sysfs   DEFAULTS        0 0
                proc                    /proc                   proc    DEFAULTS        0 0
                #10.240.253.21:/hpetl    /hpetl            nfs    DEFAULTS    0 0
                /dev/vg_app/lv_template    /template        ext4    DEFAULTS    0 0 

            p 显示替换成功的行


    高级编辑命令
        h 把模式空间中的内容覆盖至保持空间中
        H 把模式空间中的内容追加至保持空间中
        g 把保持空间中的内容覆盖至模式空间中
        G 把保持空间中的内容追加至模式空间中
        x 把模式空间中的内容与保持空间中的内容互换
        n 覆盖读取匹配到的行的下一行至模式空间中
        N 追加读取匹配到的行的下一行至模式空间中
        d 删除模式空间中的行
        D 删除多行模式空间中的所有行

本文来自投稿,不代表Linux运维部落立场,如若转载,请注明出处:http://www.178linux.com/100951

发表评论

登录后才能评论

This site uses Akismet to reduce spam. Learn how your comment data is processed.

联系我们

400-080-6560

在线咨询:点击这里给我发消息

邮件:1823388528@qq.com

工作时间:周一至周五,9:30-18:30,节假日同时也值班