在多个文件中查找并删除一行 [英] find and remove a line in multiple files
问题描述
我有很多文件,我必须在这些文件中找到一行并删除,并且应该在Linux的终端中.有人知道怎么做吗?
I have many files, and I have to find a single line in these files and delete, and should be in terminal in linux. Anyone know how to do?
示例
文件系统
myFiles
+ file1
+ file2
...
+ file6500
文件
aaa0
aaa1
....
fff9
推荐答案
这将删除每个文件中的该行.
This would delete that line in each file.
for f in myFiles/*; do
sed -i 'd/pattern that matches line that you want to delete/' $f
done
或者,您也可以使用awk.
Alternatively you could use awk as well.
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 或扩展.让我知道这是否足以回答您的问题.
The pattern here would be a regular expression. You can specify different variants of regular expressions, e.g. POSIX or extended by passing different flags to sed or awk. Let me know if this adequately answers your question.
After responding to your question, I found it to be a duplicate: Delete lines in a text file that containing a specific string
这篇关于在多个文件中查找并删除一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!