Sed is a Command Line Interfacetool for editing streams.

Lots of extra examples here.

Replace text in a file

Replace first match:

sed -i "s/old_text/MY_NEW_TEXT/" sample.txt

Replace everywhere:

sed -i "s/old_text/MY_NEW_TEXT/g" sample.txt

Replace 4th occurence:

sed -i "s/old_text/MY_NEW_TEXT/4" sample.txt

Replace text with URL in a file

Need to use a different separator (|) to not confuse sed with too many slashes (/).

export replace="http://localhost"
sed -i "s|text|$replace|g" sample.txt

Delete lines matching regex

sed '/regex/d'

Insert blank line above every line matching regex

sed '/regex/{x;p;x;}'

Insert blank line below every line matching regex

sed '/regex/G'

Remove trailing whitespace at every line ending

sed -E -e 's/\s+$//' FILENAME

Remove blank lines in a file

sed -E -e 's/^\s+$//' FILENAME