How would I use sed to delete all lines in a text file that contain a specific string?
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 <