Linux Shell Scripting Tutorial (LSST) v1.05r3 | ||
Chapter 7: awk Revisited | ||
|
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) |
/p | Print 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
Expression | Meaning |
^ | 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 |
d | Now delete all lines between *** and *** range |
You can group the commands in sed - scripts as shown following example
$ cat > dem_gsed < |
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 < |
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:
Expression | Meaning |
/^$/d | Find all blank line and delete is using d command. |
s/ */ /g | Find 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:
|
Run it as follows:
$ sed -f mkchgfrddb friends > updated_friendsdb
$ cat updated_friendsdb
Above script can be explained as follows:
Expression | Meaning |
s/A.bad/Aurangabad/g | Substitute 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/g | Substitute Maharastra for MH |
s/^$/==========/g | Substitute 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.
How to write sed scripts? | Examples of Shell Scripts |