我需要删除文件中包含某个字符串多次的所有行,例如,如果我的文件如下:
This is a test toRemove first line
This is a test toRemove second line toRemove
应该生成一个只包含第一行的文件。
This is a test toRemove first line
我正在尝试在Linux命令行上执行此操作,并尝试使用grep或sed,如下所示:
grep -d "toRemove.*toRemove" myFile > myOtherFile
sed '/\toRemove.*toRemove/!d' myFile > myOtherFile
但是似乎什么都不起作用。有谁知道如何获得这个?
sed '/toRemove.*toRemove/d' myFile > myOtherFile
。\t
匹配制表符,而!d
则删除不匹配模式的行。 - Wiktor Stribiżewgrep -v "toRemove.*toRemove" myFile > myOtherFile
应该可以正常工作。 - anubhava