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 %

  • 相关阅读:
    鸟哥私房菜*基础篇(3)
    Java 基于Graphics2D绘制电子收据图片
    微信扫码支付沙盒测试,解决沙盒环境下签名验证失败
    JAVA对象合集,根据条件过滤
    java对象根据字段进行排序
    vue 弹窗调用父窗口函数
    php使用post功能,调用微信推送服务
    JS字符串截取
    关于两个 IQueryable 合并
    bootstrap清除数据源
  • 原文地址:https://www.cnblogs.com/sea-stream/p/14067693.html
Copyright © 2011-2022 走看看