UNIX - Sed By Example

UNIX / Linux - General UNIX

Find and Replace

Action -  Will replace all words arp with /sbin/arp in the file /etc/init.d/proxyarp 

sed -i 's|arp|/sbin/arp|g' /etc/init.d/proxyarp

Find and Delete

To remove blank lines, 

sed -e '/^$/d' file.txt

Remove Multiple words

This will remove word, word1, or word2 from the file input.txt

sed 's/word\|word1\|word2//g' input.txt

Change the first instance on each line

This will substitute the 1 instance of the word dog for cat on each line.

sed 's/dog/cat/1' newfile.txt

Change case

This will change all the text to uppercase.

 sed -e 's/.*/\U&/g' input.txt

Changing multiple files

Below will change all the files in /etc from word1 to word2.

 sed -i 's/word1/word2/g' /etc/*

 

Further one-liners can be found here