查找并删除多个文件中的一行

时间:2015-04-14 03:42:08

标签: linux terminal console

我有很多文件,我必须在这些文件中找到一行并删除,并且应该在linux中的终端中。有谁知道怎么办?

实施例

文件系统

myFiles
   + file1
   + file2
   ...
   + file6500

文件

aaa0
aaa1
....
fff9

1 个答案:

答案 0 :(得分:1)

这会删除每个文件中的那一行。

for f in myFiles/*; do
  sed -i 'd/pattern that matches line that you want to delete/' $f
done

或者你也可以使用awk。

tmp=$(mktemp)
for f in myFiles/*; do
  awk '!/pattern that matches the line that you want to delete/' $f > $tmp
  cp $tmp $f 
done
rm $tmp

这里的模式是一个正则表达式。您可以指定正则表达式的不同变体,例如通过将不同的标志传递给sed或awk, POSIX 扩展。如果这足以回答你的问题,请告诉我。

在回答您的问题后,我发现它是重复的:Delete lines in a text file that containing a specific string