sed简介及常见用法

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. 

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

赞 (0)
耗奇害死猫耗奇害死猫
上一篇 2018-01-04 09:47
下一篇 2018-01-04 11:30

相关推荐

  • WORD文档格式要求与linux学习规划

    WORD文档格式要求 一、封面               1、活动名称(隶书,小初号字,加粗)               2、主办单位、策划日期(宋体、3号字) 二、目录    &nbs…

    Linux干货 2013-06-20
  • N26-博客作业-week7

    1、创建一个10G分区,并格式为ext4文件系统;      (1) 要求其block大小为2048, 预留空间百分比为2, 卷标为MYDATA, 默认挂载属性包含acl;     ~]# fdisk /dev/sda Welcome to fdisk (util-li…

    Linux干货 2017-03-19
  • 关于文件系统备份、交换分区挂载等–中

    dd命令:convert and copy a file 用法: dd if=/PATH/FROM/SRC of=/PATH/TO/DEST  bs=#:block size, 复制单元大小 count=#:复制多少个bs  of=file 写到所命名的文件而不是到标准…

    Linux干货 2016-08-29
  • 磁盘管理及shell脚本编程练习

    1、创建一个10G分区,并格式为ext4文件系统 a) 要求其block大小为2048,预留空间百分比为2,卷标为MYDATA,默认挂载属性包含acl; ~]# mke2fs –t ext4 –b 2048 –m 2 –L MYDATA  /dev/sda3 b) 挂载至/data/madata目录,要求挂载时禁止程序自动运行,且不更新文件的访问时间戳; ~…

    2017-11-15
  • vim编辑器

                                                      &nbsp…

    Linux干货 2015-12-19
  • scp命令介绍

    scp是secure copy的简写,用于在Linux下进行远程拷贝文件的命令,和它类似的命令有cp,不过cp只是在本机进行拷贝不能跨服务器,而且scp传输是加密的。可能会稍微影响一下速度。当你服务器硬盘变为只读 read only system时,用scp可以帮你把文件移出来。另外,scp还非常不占资源,不会提高多少…

    Linux干货 2017-07-24

评论列表(1条)

  • 马哥教育
    马哥教育 2018-01-07 18:02

    博文内容上简单了点,sed还是有蛮多知识可以总结的~可以扩充一下,继续加油哈~