bash 如何删除文件夹的所有文本文件中包含单词的行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15688733/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to delete a line that contains a word in all text files of a folder?
提问by AndroidSec
So, in linux, I have a folder with lots of big text files.
所以,在 linux 中,我有一个文件夹,里面有很多大文本文件。
I want to delete all the lines of these files that contain a specific keyword. Is there any easy way to do that across all files?
我想删除这些文件中包含特定关键字的所有行。有没有什么简单的方法可以在所有文件中做到这一点?
回答by Jo So
There already many similar answers. I'd like to add that if you want to match this is a line containing a keywordbut not this is a line containing someoneelseskeyword, then you had better added brackets around the word:
已经有很多类似的答案了。我想补充一点,如果您想匹配this is a line containing a keyword但不想匹配this is a line containing someoneelseskeyword,那么您最好在单词周围添加括号:
sed -i '/\<keyword\>/d' *.txt
回答by Steven Penny
I cannot test this right now, but it should get you started
我现在不能测试这个,但它应该让你开始
find /path/to/folder -type f -exec sed -i '/foo/d' {} ';'
- find files in the directory
/path/to/folder - find lines in these files containing
foo - delete those lines from those files
- 在目录中查找文件
/path/to/folder - 在这些文件中找到包含的行
foo - 从这些文件中删除这些行
回答by nitin
sed -i '/keyword/d' *.txt -- run this in your directory.
sed -i '/keyword/d' *.txt -- 在你的目录中运行它。
sed - stream editor , use it here for deleting lines in individual files
sed - 流编辑器,在这里使用它来删除单个文件中的行
-i option : to make the changes permenent in the input files
-i 选项:在输入文件中进行永久性更改
'/keywprd/' : specifies the pattern or the key to be searched in the files
'/keywprd/' : 指定要在文件中搜索的模式或键
option d : informs sed that matching lines need to be deleted.
选项 d :通知 sed 需要删除匹配的行。
*.txt : simply tells sed to use all the text files in the directory as input for
processing , you can specify a individual or a extension like *.txt the way i did.
*.txt :简单地告诉 sed 使用目录中的所有文本文件作为
处理的输入,您可以像我一样指定个人或扩展名 *.txt。
回答by Gary
try this:
尝试这个:
find your_path_filename |xargs sed -i '/key_word/d'
回答by mplwork
Sure:
当然:
for x in files*
do
grep -v your_pattern "$x" > x && mv x "$x"
done

