sed > Stream editor allow filter and transform texts, find pattern and replace with whatever is that you want to replace with, it's like search and replace.

You can use other separators instead of slashes (/). Example pipe | or hash #

1 - sed substitution -- s for substitution. replaces only the first occurrence in each line of the file and save to a new file

sed 's/word/newword/' < oldfile > newfile

 

2 - replaces every occurrence of 'word'. g for global

sed 's/word/newword/g' < oldfile > newfile

 

3 - grab input from command and change it. It's searching for strings and not words

echo "Renata" | sed 's/Renata/Renny/'

echo "Renata Brownish" | sed 's/Brown/Red'

 

4 - Using -i flag changes will be made in the original file

sed -i 's/word/newword/g' filename

 

5 - find all the lines with a specific 'Pattern' and substitute a word in that line with new word.

Find the pattern first, then substitute the word 'that' in the lines where you find my pattern with 'THAT'

sed '/pattern/s/that/THAT/'  myfile.txt  ---- only first occurrence in the line

sed '/pattern/s/that/the/g'  myfile.txt    ----every occurrence in the line

 

6 - Delete the lines that appear the pattern / word 'unique' - it's not a persistent delete though unless you use the flag -i

sed '/unique/d' myfile.txt

Delete an exact match from /etc/hosts file: Use -i flag to make it persistent

sed '/nsp-edge-drcn.platform.dbankcloud.cn/d' test-hosts.txt

 

sed '/192.98.85.100/d' ips.txt