Syntax:
sed -i '/string_to_delete/d' /path/to/file
SED is a Stream Editor having the capability to remove lines from files containing a specific string. Using -i with sed we can remove lines in the same file.
Advertisement
Example 1:
Remove all lines from /var/log/messages containing the string “DELETE THIS TEXT” and restore output in a new file. Do not make any changes to the original line.
$ sed "/DELETE THIS TEXT /d" /var/log/messages > messages.txt
Example 2:
Remove all lines from /var/log/messages containing the string “DELETE THIS TEXT” in the same file.
$ sed-i "/DELETE THIS TEXT /d" /var/log/messages
2 Comments
It’s important to know how to do the redirection method because I work with older systems, like Solaris, and their version of sed is not so forgiving. I usually output (>) to a file like “file.tmp”, then “mv file.tmp file”
I would add that: sed -i.tmp ‘/^startswith/d’ /dir/file would be optimal. I end up needing to delete files but not using that I often have mixed results on different OS’s. I think with the more modern systems you may be able to get away with -i alone?
You can quote in ‘ or “. It would depend if you are want interpolation or not. Double quotes if you have a variable.
The syntax is incorrect. replace ‘ with ”
sed -i “/Text to Delete/d’” /path/to/file