• shell sed 删除行


    sed编辑器不会修改原始文件。你删除的行只是从sed编辑器的输出中消失了。原始文件仍然包含那些“删掉的”行

    macname@localhost Desktop % cat data1 
    1The quick brown fox jumps over the lazy dog
    2The quick brown fox jumps over the lazy dog
    3The quick brown fox jumps over the lazy dog
    4The quick brown fox jumps over the lazy dog
    5The quick brown fox jumps over the lazy dog
    6The quick brown fox jumps over the lazy dog
    7The quick brown fox jumps over the lazy dog
    macname@localhost Desktop %


    删除第二行

    macname@localhost Desktop % sed '2d' data1 
    1The quick brown fox jumps over the lazy dog
    3The quick brown fox jumps over the lazy dog
    4The quick brown fox jumps over the lazy dog
    5The quick brown fox jumps over the lazy dog
    6The quick brown fox jumps over the lazy dog
    7The quick brown fox jumps over the lazy dog

    删除第二行和第三行

    macname@localhost Desktop % sed '2,3d' data1 
    1The quick brown fox jumps over the lazy dog
    4The quick brown fox jumps over the lazy dog
    5The quick brown fox jumps over the lazy dog
    6The quick brown fox jumps over the lazy dog
    7The quick brown fox jumps over the lazy dog

    删除第二行和第三行

    macname@localhost Desktop % 
    macname@localhost Desktop % cat ddd
    一The quick brown fox jumps over the lazy dog
    二The quick brown fox jumps over the lazy dog
    三The quick brown fox jumps over the lazy dog
    四The quick brown fox jumps over the lazy dog
    五The quick brown fox jumps over the lazy dog
    六The quick brown fox jumps over the lazy dog
    七The quick brown fox jumps over the lazy dog
    macname@localhost Desktop % 
    macname@localhost Desktop % sed '2,3d' ddd
    一The quick brown fox jumps over the lazy dog
    四The quick brown fox jumps over the lazy dog
    五The quick brown fox jumps over the lazy dog
    六The quick brown fox jumps over the lazy dog
    七The quick brown fox jumps over the lazy dog
    
     

    删除第三行,以及之后的所有行

    macname@localhost Desktop % sed '3,$d' data1 
    1The quick brown fox jumps over the lazy dog
    2The quick brown fox jumps over the lazy dog

    删除第二行和第五行,以及这两行之间的所有行

    macname@localhost Desktop % sed '/2/,/5/d' data1
    1The quick brown fox jumps over the lazy dog
    6The quick brown fox jumps over the lazy dog
    7The quick brown fox jumps over the lazy dog
    macname@localhost Desktop %

    这里是通过内容匹配的

    macname@localhost Desktop % cat ddd
    一The quick brown fox jumps over the lazy dog
    二The quick brown fox jumps over the lazy dog
    三The quick brown fox jumps over the lazy dog
    四The quick brown fox jumps over the lazy dog
    五The quick brown fox jumps over the lazy dog
    六The quick brown fox jumps over the lazy dog
    七The quick brown fox jumps over the lazy dog
    macname@localhost Desktop % 
    macname@localhost Desktop % sed '/2/,/5/d' ddd
    一The quick brown fox jumps over the lazy dog
    二The quick brown fox jumps over the lazy dog
    三The quick brown fox jumps over the lazy dog
    四The quick brown fox jumps over the lazy dog
    五The quick brown fox jumps over the lazy dog
    六The quick brown fox jumps over the lazy dog
    七The quick brown fox jumps over the lazy dog
    macname@localhost Desktop % sed '/二/,/五/d' ddd
    一The quick brown fox jumps over the lazy dog
    六The quick brown fox jumps over the lazy dog
    七The quick brown fox jumps over the lazy dog
    macname@localhost Desktop %


    除此之外,你要特别小心,因为只要sed编辑器在数据流中匹配到了开始模式,删除功能就 会打开。这可能会导致意外的结果。
    第二个出现数字“1”的行再次触发了删除命令,因为没有找到停止模式,所以就将数据流中的剩余行全部删除了。

    macname@localhost Desktop % cat data1 
    1The quick brown fox jumps over the lazy dog
    2The quick brown fox jumps over the lazy dog
    3The quick brown fox jumps over the lazy dog
    The quick brown fox jumps over the lazy 1 dog
    4The quick brown fox jumps over the lazy dog
    5The quick brown fox jumps over the lazy dog
    6The quick brown fox jumps over the lazy dog
    7The quick brown fox jumps over the lazy dog
    macname@localhost Desktop % 
    macname@localhost Desktop % sed '/1/,/3/d' data1 
    macname@localhost Desktop % 
    macname@localhost Desktop %

    当然,如果你指定了一个从未在文本中出现的停止模式,显然会出现另外一个问题。
    因为删除功能在匹配到第一个模式的时候打开了,但一直没匹配到结束模式,所以整个数据 流都被删掉了。

    macname@localhost Desktop % 
    macname@localhost Desktop % sed '/1/,/10/d' data1
    macname@localhost Desktop %

  • 相关阅读:
    redhat6.4 数据包无法到达
    hibernate-Table 'XXX.XXX' doesn't exist
    LeetCode 之 TwoSum
    vim 中的常用编辑
    linux sed 批量替换多个文件中的字符串
    RedHat 6.4企业版利用iso镜像做本地yum源
    win7 vmware虚拟机上网设置
    virtualbox ubuntu下ssh连接
    Source Insight 插件
    非递归排序
  • 原文地址:https://www.cnblogs.com/sea-stream/p/14067693.html
Copyright © 2020-2023  润新知