Linux Shell Scripting Tutorial (LSST) v1.05r3
Prev
Chapter 7: awk Revisited
Next

More examples of sed

First create text file demofile2 which is used to demonstrate next sed script examples.
Type following sed command at shell promote:
$ sed -n '/10\{2\}1/p' demofile2
1001
Above command will print 1001, here in search pattern we have used \{2\}.
Syntax:
\{n,\}
At least nth occurrences will be matched. So /10\{2\} will look for 1 followed by 0 (zero) and \{2\}, tells sed look for 0 (zero) for twice.

Matcheing any number of occurrence

Syntax:
\{n,\m}
Matches any number of occurrence between n and m.

Example:
$ sed -n '/10\{2,4\}1/p' demofile2
1001
10001
100001
Will match "1001", "10001", "100001" but not "101" or "10000000". Suppose you want to print all line that begins with *** (three stars or asterisks), then you can type command
$ sed -n '/^\*..$/p' demofile2
***
***
Above sed expression can be explianed as follows:

Command
Explnation
^Beginning of line
\* Find the asterisk or star (\ remove the special meaning of '*' metacharacter)
..

Followed by any two character (you can also use \*\* i.e. $ sed -n '/^\*\*\*$/p' demofile2 )

$

End of line (So that only three star or asterisk will be matched)

/pPrint the pattern.

Even you can use following expression for the same purpose
$ sed -n '/^\*\{2,3\}$/p' demofile2

Now following command will find out lines between *** and *** and then delete all those line
$sed -e '/^\*\{2,3\}$/,/^\*\{2,3\}$/d' demofile2 > /tmp/fi.$$
$cat /tmp/fi.$$


Above expression can be explained as follows

ExpressionMeaning
^Beginning of line
\* Find the asterisk or star (\ remove the special meaning of '*' metacharacter) 
\{2,3\}Find next two asterisk 
$End of line
,Next range or search pattern
^\*\{2,3\}$Same as above
dNow delete all lines between *** and *** range

You can group the commands in sed - scripts as shown following example

$ cat > dem_gsed
/^\*\{2,3\}$/,/^\*\{2,3\}$/{
/^$/d
s/Linux/Linux-Unix/
}
<

Now save above sed script and run it as follows:
$ sed -f dem_gsed demofile2 > /tmp/fi.$$
$ cat /tmp/fi.$$

Above sed scripts finds all line between *** and *** and performance following operations
1) Delete blank line, if any using /^$/d expression.
2) Substitute "Linux-Unix" for "Linux" word using s/Linux/Linux-Unix/ expression.

Our next example removes all blank line and converts multiple spaces into single space, for this purpose you need demofile3 file. Write sed script as follows:

$ cat > rmblksp
/^$/d
s/ */ /g
<

Run above script as follows:
$ sed -f rmblksp demofile3
Welcome to word of sed what sed is?
I don't know what sed is but I think
Rani knows what sed Is
--------------------------------------------------

Above script can be explained as follows:

ExpressionMeaning
/^$/dFind all blank line and delete is using d command.
s/   */ /gFind two or more than two blank space and replace it with single blank space

Note that indicates     two blank space and indicate   one blank space.

For our next and last example create database file friends
Our task is as follows for friends database file:
1)Find all occurrence of "A'bad" word replace it with "Aurangabad" word
2)Exapand MH state value to Maharastra
3)Find all blank line and replace with actual line (i.e. ========)
4)Instert e-mail address of each persons at the end of persons postal address. For each person e-mail ID is different

To achieve all above task write sed script as follows:

$ cat > mkchgfrddb
s/A.bad/Aurangabad/g
s/MH/Maharastra/g
s/^$/===================================================================/g
/V.K. /{
N
N
a\
email:vk@fackmail.co.in
}

/M.M. /{
N
N
a\
email:mm@fackmail.co.in
}

/R.K. /{
N
N
a\
email:rk@fackmail.co.in
}

/A.G. / {
N
N
a\
email:ag@fackmail.co.in
}

/N.K. / {
N
N
a\
email:nk@fackmail.co.in
}

Run it as follows:
$ sed -f mkchgfrddb friends > updated_friendsdb
$ cat updated_friendsdb

Above script can be explained as follows:

ExpressionMeaning
s/A.bad/Aurangabad/gSubstitute Aurangabad for A'bad. Note that here second character in A'bad is ' (single quote), to match this single quote we have to use . (DOT - Special Metacharcter) that matches any single character.
s/MH/Maharastra/gSubstitute Maharastra for MH
s/^$/==========/gSubstitute blank line with actual line
/V.K. /{
N
N
a\
email:vk@fackmail.co.in
}
Match the pattern and follow the command between { and }, if pattern found. Here we are finding each friends initial name if it matches then we are going to end of his address (by giving N command twice) and appending (a command) friends e-mail address at the end.

Our last examples shows how we can manipulate text data files using sed. Here our tutorial on sed/awk ends but next version (LSST ver 2.0) will cover more real life examples, case studies using all these tools, plus integration with shell scripts etc.


Prev
Home
Next
How to write sed scripts?
Up
Examples of Shell Scripts