How to delete from a text file, all lines that contain a specific string?

后端 未结 18 2149
生来不讨喜
生来不讨喜 2020-11-22 02:06

How would I use sed to delete all lines in a text file that contain a specific string?

18条回答
  •  迷失自我
    2020-11-22 02:30

    You can use good old ed to edit a file in a similar fashion to the answer that uses ex. The big difference in this case is that ed takes its commands via standard input, not as command line arguments like ex can. When using it in a script, the usual way to accomodate this is to use printf to pipe commands to it:

    printf "%s\n" "g/pattern/d" w | ed -s filename
    

    or with a heredoc:

    ed -s filename <

提交回复
热议问题