【问题标题】:delete lines in a files using sed [duplicate]使用 sed 删除文件中的行 [重复]
【发布时间】:2012-03-28 09:39:38
【问题描述】:

可能重复:
eliminate unwanted output using awk and sed

以下内容存在于一个文件中。如何删除以find: Filesystem loop detected; 开头的所有行?

find: Filesystem loop detected; `/nfs/.snapshot/nightly.4' has the same device number and inode as a directory which is 2 levels higher in the filesystem hierarchy. 
find: Filesystem loop detected; `/nfs/.snapshot/nightly.5' has the same device number and inode as a directory which is 2 levels higher in the filesystem hierarchy. 
find: Filesystem loop detected; `/nfs/.snapshot/nightly.6' has the same device number and inode as a directory which is 2 levels higher in the filesystem hierarchy.
Owner     RepoName             CreatedDate

asasasa s sasasas 

【问题讨论】:

    标签: linux unix sed awk


    【解决方案1】:

    为什么不grep

    grep -v '^find: Filesystem loop detected;' myFile
    

    【讨论】:

      【解决方案2】:

      sed:

       sed '/^find: Filesystem loop detected/d' file
      

      awk:

      awk '!/^find: Filesystem loop detected/' file
      

      grep 也是一个不错的选择,正如 mouviciel 指出的那样。

      【讨论】:

        【解决方案3】:

        有多种方法。

        使用sed,最简单的方法大概是:

        sed -n '1,/find: Filesystem loop detected/p' /path/to/logfile
        

        将输出重定向到一个临时文件,将其移动到日志文件的位置,然后重新启动 syslogd。

        关于命令的工作原理:

        • sed -n 是“除非有指示,否则不打印行”
        • 1,是“从第 1 行开始直到...”
        • /.../ "此正则表达式匹配"
        • p 是“打印线”

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-04-26
          • 1970-01-01
          • 2021-12-13
          • 1970-01-01
          • 1970-01-01
          • 2020-04-02
          相关资源
          最近更新 更多