下面的命令将查找所有出现的字符串并删除找到该字符串的文件的内容。
find / -maxdepth 1 -xdev -type f -exec grep -i "stringtofind" -l {} \; -exec sed -i '/./d' {} \;
我尝试修改它以仅删除找到该字符串的行,但无法使其正常工作。
例如测试文件:
blah blah blah
blah blah blah teststring
teststringblah blah blah
blah blah blah
它会删除第 2 行和第 3 行,并使文件的行与行之间没有间隙:
blah blah blah
blah blah blah
最佳答案
这里不需要 grep
sed -i '/teststring/Id' file
删除 file
中包含 teststring 的所有行
(不区分大小写) 所以只需将它与 find
结合起来:
find . -maxdepth 1 -xdev -type f -exec sed -i '/teststring/Id' {} \;
sed
演示:
$ cat file
blah blah blah
blah blah blah teststring
teststringblah blah blah
blah blah blah
$ sed '/teststring/Id' file
blah blah blah
blah blah blah
关于linux - 如何 grep 整行,传递给 sed,然后删除该行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13859684/