UNIX - Sed By Example
Wednesday, 24 September 2008 10:14

Find and Replace

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

Find and Delete

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/*

Additonal

Further one-liners can be found here



 
We have 178 guests online