使用sed删除文件中的行

时间:2012-03-28 09:39:38

标签: linux unix sed awk

  

可能重复:
  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 

3 个答案:

答案 0 :(得分:2)

为什么不grep

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

答案 1 :(得分:1)

sed的:

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

AWK:

awk '!/^find: Filesystem loop detected/' file
正如mouviciel指出的那样,grep也是一个不错的选择。

答案 2 :(得分:0)

有多种方式。

使用sed,最简单的方法可能是:

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

将outpout重定向到临时文件,移动该文件代替日志文件,然后重新启动syslogd。

关于命令如何工作:

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