sed简介

作者: 耗奇害死猫 | 来源:发表于2018-01-04 10:06 被阅读0次

    sed是一个文本流处理器,配合正则表达式用可以实现很多文本处理操作。

    和grep一样,sed是一行一行的处理的。sed处理文本时,首先会将源文件复制一份到内存中,然后将文本一行一行拿到模式空间内进行操作,最后输出到标准输出,即屏幕上。

    在模式空间中,每一行都会根据用户给的条件进行匹配,匹配到了进行编辑后输出,没有匹配到,直接输出到标准输出。sed除了模式空间还有一个保持空间,能够让行在模式空间和保持空间多次进行处理,进而完成复杂的处理工作。

    sed [option]...'script' [input-file]...
        script:
            地址定界、编辑命令
        常用选项:
            -n, --quiet, --slient:不输出模式空间中的内容到屏幕,也就是默认
                suppress automatic printing of pattern space:自动不输出模式空间中的内容
            -e script, --expression=script:添加可执行的脚本,用脚本来处理文本,多点编辑
                add the script to the commands to be executed.
            -f script, --expression=script:
                add the contents of script-file to the commands to be executed
            -r. --regxp-extended:支持使用扩展正则表达式
            -i[SUFFIX], --in-place[=SUFFIX]:直接编辑源文件
        地址定界:
            1.空地址:全文处理
            2.单地址:
                # :第#行
                /patter/ :被此模式匹配到的每一行
            3.地址范围:
                #,# :
                #,+# :从第#行开始的第#行
                #,/pat1/ :从第#行开始,到第一次匹配到模式的行
                /pat1/,/pat2/ :从/pat1/到/pat2/
            4.步进:~
                1~2:所有奇数行
    
        编辑信息(COMMANDS):
        除了用d模式,都用上-n选项
            d :将匹配到的行删除;
            p :print the current pattern space
                输出当前模式空间中的内容
            a \text:
                append text, which has each embedded newline preceded by a backslash
                追加新行,反斜线前的范围内每一行后都追加新的一行。可以用\n实现多行追加
            i \text:
                insert text,which has each embedded newline preceded by a backslash
                插入新行,反斜线前指范围内每一行前插入新的一行。可以用\n实现多行追加
            c \text:
                replace the selected lines with text,which has each embedded newline preceded by a backlash.
                将指定的行更换文本,~。~
            w /path/to/somefile:
                write the current pattern space to filename
                保存模式空间匹配到的行至指定的文件中;
            r /path/to/somefile:
                append text read from filename
                文件合并
            = :为模式匹配到的行打印行号
            ! :条件取反,在COMMAND前用;
                地址定界!编辑命令
            s/// :查找替换,其分隔符可自行指定,常用 s@@@,s###等
                替换标记:
                    g :全局替换
                    W /path/to/somefile:将替换成功的结果保存至指定文件中;
                    P :显示替换成功的行
    

    在hold space(保持空间)中还有一些高级命令:

    高级编辑命令:
        h H    copy/append pattern space to hold space.
        g G    Copy/append hold space to pattern space.
        x      Exchange the contents of the hold and pattern spaces.
        n N    Read/append the next line of input into the pattern space.
        d      Delete pattern space.  Start next cycle.
        D      If  pattern  space contains no newline, start a normal new cycle as if the d command was issued.  Otherwise, delete text in the pattern space up  to  the first  newline,  and restart cycle with the resultant pattern space, without reading a new line of input.
    

    相关文章

      网友评论

          本文标题:sed简介

          本文链接:https://www.haomeiwen.com/subject/wcglnxtx.html