According to sed’s man page…
Sed is a stream editor. A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline). While in some ways similar to an editor which permits scripted edits (such as ed), sed works by making only one pass over the input(s), and is consequently more efficient. But it is sed's ability to filter text in a pipeline which particularly distinguishes it from other types of editors.
Next, we are going to explore some of the basic usage of sed.
Substitute every occurrence of string1 with string2. The g stands for global, which means that all matching occurrences in the line would be replaced and the output would be directed to outputFile instead of the default standard output.
sed ‘s/string1/string2/g’ inputFile > outputFile
From inputFile show me lines 5732 to 5797. Note the importance of the n switch and the p command to print only the range of lines specified. Without n sed will print the entire file:
sed -n ’5732,5797p’ inputFile
Now I want sed to print everything but lines 5732 to 5797:
sed ’5732,5797d’ inputFile
A kind of cat inputFile1 >> inputFile2:
sed r inputFile1 inputFile2
Show me a paragraph of inputFile that begins with the words “simple mail transport protocol” and ends with the word “postfix”:
sed -n ‘/simple mail transport protocol/,/postfix/p’ inputFile
Next trick will delete empty lines or lines that only contain space characters. Note that the caret denotes the begining of a line, de dollar symbol denotes end of line, the period a single character and asterisk matches zero or more occurrences of the previous character.
sed -e ‘/^ *$/d’ inputFile
Add blank lines to inputFile:
sed ‘G’ inputFile > outputFile
And now delete those blank lines:
sed ‘n;d’ outputFile
A kind of cat inputFile | wc -l:
sed -n ‘$=’ inputFile
Number each line of inputFile:
sed = inputFile | sed ‘N;s/n/t/’
References: