【发布时间】:2021-08-09 16:37:56
【问题描述】:
我需要删除文件中多次包含某个字符串的所有行,例如,如果我的文件是这样的:
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匹配 TAB 字符,!d删除不匹配模式的行 -
grep -v "toRemove.*toRemove" myFile > myOtherFile应该可以工作。
标签: regex sed command-line grep