Using sed to delete multiple lines from a file in linux
Enter the multiline sed
There are two ways to do this, I'm going to go over the simpler one here, since if you are reading this, you probably need this information now:
Replace or remove multiple lines from a text file (the easy way):
sed '/FIRSTLINETODELETE/,/LASTLINETODELETE/d' /the/file/to/delete/from
*Lines can be regex, but make sure you get the whole line
Note: if you are cleaning script injections with a unique "tag" in the comment before and after the injection, just replace UNIQUETAG below with the random characters in the comment:
user@host [~]# sed '/<!--UNIQUETAG-->/,/<!--UNIQUETAG-->/d' /your/injected/file
This will output what the file will look like, but sed without -i will not modify the file.
If the output of the above looks correct, add -i after sed:
sed -i WILL modify the original file - please be sure your output is correct
sed -i '/<!--UNIQUETAG-->/,/<!--UNIQUETAG-->/d' /your/injected/file"Regular" sed command, operates on a single line by default:
user@host [~]# sed 's/<!--UNIQUETAG.*UNIQUETAG-->//' /your/injected/fileThis version will NOT work across multiple lines
*Note: I'm working in Centos but this should all work the same on most Linux distros. Also, for completeness' sake the other and "proper" way to do multi-line sed is the N option, which allows you to do much more complex tasks. This example deletes the last 2 lines of a file:
user@host [~]# sed 'N;$!P;$!D;$d' fileName
In over your head? Call a Developer for help with Linux, servers, code, websites, and more. Let us worry about the tech stuff while you move your business forward.