zoukankan      html  css  js  c++  java
  • 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 %

  • 相关阅读:
    SQL SERVER列转换行及UNPIVOT
    25.元素,元素内容和元素属性 Walker
    16.开发工具介绍 Walker
    13.前后端程序浅解 Walker
    21.创建标准的html文件 Walker
    15.html和html5 Walker
    24.代码注释 Walker
    22.文档的基本结构 Walker
    23.单标签和双标签 Walker
    26.代码书写规范 Walker
  • 原文地址:https://www.cnblogs.com/sea-stream/p/14067693.html
Copyright © 2011-2022 走看看