Learn SED with examples

Before going to learn SED a brief intro is required.

a)sed is a Stream EDitor.

b)It reads line by line when a file is feed to it.

c)sed will not edit the input file by default and it displays output on the screen.

d)SED is mainly used for search for a term and replace with desired term in a file/stream

SED can do following things.

1) Search and replace (s switch) –90 % of your SED related work is completed with this option

2) Printing (-n and p switch)

3) Editing (-I, w, d switch)

4) Multiple SED commands and continuation operation (-e and ; switch)

5) Line number ( , and = switch)

6) Search operation (/searcterm/)

7) Negation operation(!)

8) SED scripting operator (-f switch)

9) Miscellaneous SED examples

Search and replace operator( s for search)

Below is the syntax for search a term and replace it with another team.

sed ‘s/searchterm/replaceterm/’ inputfile

or

cat inputfilename | sed ‘s/searchterm/replaceterm/’

or

echo “This is test message” | sed ‘s/searchterm/replaceterm/’

Exameple1: Search for google in file:test.txt and replace with yahoo

sed ‘s/google/yahoo/’ test.txt

Example2: How about globally? By default sed will work on only first find term. Suppose I have following sentence.

“sheena leads, sheila needs”. I want to replace sh with le. If I use below example it will replace only first occurrence.

echo “sheena leads, sheila needs” | sed ‘s/sh/le/’

Output

leeena leads, Sheila needs

Solution is g switch, to inform sed to search for the term globally/multiple times within the line.

echo “sheena leads, sheila needs” | sed ‘s/sh/le/g’

Output

Leeena leads, leeeila needs

Example3: How about changing separator: This very much useful when your search term contains separator. At that time your SED can not understand. For example you want to search for /var/ftp/pub and replace it with /opt/ftp/com. The below code will not work properly.

sed ‘s//var/ftp/pub//opt/ftp/com/’ test.txt

For this you should change the separator from / to some other char such as # or $. In fact any character which is not present in search and replace term. For above example this will work.

sed ‘s_/var/ftp/pup_/opt/ftp/com_’ test.txt

Here my separator is _

Example4: Whenever you search for a term, your sed will keep this search term in special character ‘&’, so that you can use it in replace term. Suppose you want to search for Surendra and replace it with Mr. Surendra use below command

sed ‘s/surendra/Mr. &/’ test.txt

& for matched character and surendra is stored in & by default.

From here after if you want to get grip on SED you should know RegExp. Please find RegExp basics here and here.

Example5: () used to match group of characters. And 1 is to store that value.If there are many such they are stored in 2, 3 till 9 respectivly. i.e. First bracket value in 1, second in 2 and so on up to 9

I have a pattern like

Abc123suri

And this should be changed to

suriabc123

echo “abc123suri” | sed ‘s/([a-z]*)([0-9]*)([a-z]*)/312/’

Note:As mention earlier to know more about this you have to learn RegExp using grep here and here.

 Printing operator (-n and p switch)

Example6: SED by default display all the lines when working on them. But there are some times which require only printing of changed lines then use –n(quite output i.e no output) and p option to print only changed lines. I will take below file as my input file.

cat tem.txt

surendra audi

kumar nudi

mouni surendra

baby dudy

cat tem.txt | sed 's/surendra/bca/'

Output

surendra audi

kumar nudi

mouni bca

baby dudy

With –n(Suppress output) and p(Print the matched pattern) options

cat tem.txt | sed -n 's/surendra/bca/p'

Output:

bca audi

mouni bca

Editing operators (-i, w, d switch and Shell redirection operators)

Example7: I want to make changes to the file which is fed as input file ie changes are written to original file. To do this we have to use –i for inserting.

sed –I ‘s/bca/Surendra/’ tem.txt

Caution: Use –i option wisely. Because you cannot revert back the changes of original file.

Example8: With output redirection we can do the same thing.

sed ‘s/baby/dady/’ < tem.txt > abc.txt.

Example9: How about writing only changes to another file for future reference?

With w option, we will get only changes to the new file

sed ‘s/baby/dady/w abc.txt’ tem.txt

Example10: How about deleting lines?. Suppose you want to delete line number 3 use the below command

sed ‘3d’ tem.txt

Example10:How about deleting 4 line and update the original file?

sed –i ‘4d’ tem.txt

Caution:Use –i option wisely whenever you are using it. We will play more with lines and numbers in coming post.

Multiple SED commands and continuation operators (-e and ; switch)

Sometimes there are cases where we want to search for multiple patterns and replace these patterns with respective patterns. Suppose I want to find Surendra, mouni, baby and replace them with bca, mca, bba respectively in tem.txt. We can use below command chaining.

cat tem.txt | sed ‘s/Surendra/bca/’ | sed ‘s/mouni/mca/’ | sed ‘s/baby/bba/’

This is not a professional way to write. There is an –e option to do this multiple substitutions.

Example11: Execute multiple commands with –e option

sed –e ‘s/surendra/bca/’ –e ‘s/mouni/mca/’ –e ‘s/baby/bba/’ tem.txt

Example12:How about reducing it more by using ;(Continuation operator) for the same question?

sed ‘s/Surendra/bca/;s/mouni/mca/;s/baby/bba/’ tem.txt
Click here to find the second part of three part series of SED..