如何删除unix sh中以"string"开头的所有行?

t3h*_*man 25 unix sed file sh

如何删除sh中以"string"开头的文件中的所有行?我在考虑使用sed命令.

Mar*_*c B 31

grep -v '^string' yourfile.txt > stripped.txt
Run Code Online (Sandbox Code Playgroud)


Wil*_*ell 27

要做到这一点,如果你的sed支持-i选项,你可以这样做:

sed -i '/^string/d' input-file

  • 当使用`-i`时,sed期望备份的文件扩展名,在OS X中你必须非常迂腐,所以明确地使用单引号:`sed -i'''/ ^ string/d'input-file` (2认同)
  • 在OSX中,默认的`sed`甚至不支持`-i`!我个人的偏好是永远不要使用`-i`而是用shell重定向. (2认同)