How do you delete all lines that begin with “string” in unix sh?

前提是你 提交于 2019-12-17 23:23:12

问题


How do you delete all lines in a file that begin with "string" in sh? I was thinking about using the sed command.


回答1:


grep -v '^string' yourfile.txt > stripped.txt



回答2:


To do it in place, if your sed supports the -i option, you can do:

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



回答3:


sed and grep in your answers are missing their friend awk:

awk '!/^string/' inputfile > resultfile



回答4:


You can use Vim in Ex mode:

ex -sc g/^string/d -cx file
  1. g select all matching lines

  2. d delete

  3. x save and close



来源:https://stackoverflow.com/questions/8068203/how-do-you-delete-all-lines-that-begin-with-string-in-unix-sh

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!