Example of using Sed
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.
One of sed's most useful commands is the substitution command. Using it, we can replace a particular string or matched regular expression with another string. For example say we have a file (called test.txt) with the following content:
aaa bbb ccc
eee fff ggg
bbb ccc aaa
eee rrr kkk
We want to change all the instances of “aaa” to “zzz”. The following sed command could be used to perform the search and replace:
sed -e 's/aaa/zzz/g' test.txt > out.txt
The contents of the output file (out.txt) now has the following:
zzz bbb ccc
eee fff ggg
bbb ccc zzz
eee rrr kkk
Labels: unix