Wednesday, September 21, 2011

sed - Selective printing



    In this sed article, we are going to see the different options sed provides to selectively print contents in a file.  Let us take a sample file with the following contents:
$ cat file
Gmail 10
Yahoo 20
Redif 18
1. To print the entire file contents:
$ sed '' file
Gmail 10
Yahoo 20
Redif 18
2. To print only the line containing 'Gmail'. In other words, to simulate the grep command:
$ sed '/Gmail/p' file
Gmail 10
Gmail 10
Yahoo 20
Redif 18
 Within the slashes, we specify the pattern which we try to match. The 'p' command tells to print the line. Look at the above result properly, the line Gmail got  printed twice. Why? This is because the default behavior of sed is to print every line after parsing it. On top of it, since we asked sed to print the line containing the pattern 'Gmail' explicitly by specifying 'p", the line 'Gmail' got printed twice. How to get the desired result now?
$ sed -n '/Gmail/p' file
Gmail 10
   The desired result can be obtained by suppressing the default printing which can be done by using the option "-n". And hence the above result.

3. To delete the line containing the pattern 'Gmail'. In other words, to simulate the "grep -v" command option in sed:
$ sed  '/Gmail/d' file
Yahoo 20
Redif 18
   The "d" command denotes the delete the pattern. As said earlier, the default action of sed is to print. Hence, all the other lines got printed, and the line containing the pattern 'Gmail' got deleted since we have specified explicit "d" option.

   In the same lines, say to delete the first line of the file:
$ sed '1d' file
Yahoo 20
Redif 18
4. Print lines till you encounter a specific pattern, say till 'Yahoo' is encountered.
$ sed  '/Yahoo/q' file
Gmail 10
Yahoo 20
    The "q" command tells to quit from that point onwards. This sed command tells to keep printing(which is default) and stop processing once the pattern "Yahoo" is encountered.

Printing Range of Lines:

  Till now, what we saw is to retrieve a line or a set of lines based on a condition. Now, we will see how to get the same for a given range:

Consider the below sample file:
$ cat file
Gmail 10
Yahoo 20
Redif 18
Inbox 15
Live  23
Hotml 09
5. To print the first 3 lines, or from lines 1 through 3:
$ sed -n '1,3p' file
Gmail 10
Yahoo 20
Redif 18
  The option "-n" suppresses the default printing.  "1,3p" indicates to print from lines 1 to 3.

The same can also be achieved through:
$ sed '3q' file
Gmail 10
Yahoo 20
Redif 18
   3q denotes to quit after reading the 3rd line. Since the "-n" option is not used, the first 3 lines get printed.

6. Similar to give line number ranges, sed can also work on pattern ranges. Say, to print from lines between patterns "Yahoo" and "Live":
$ sed -n '/Yahoo/,/Live/p' file
Yahoo 20
Redif 18
Inbox 15
Live  23
  The pattern is always specified between the slashes. The comma operator is used to specify the range. This command tells to print all those lines between the patterns "Yahoo" and 'Live".

7. To print the lines from pattern "Redif" till the end of the file.
$ sed -n '/Redif/,$p' file
Redif 18
Inbox 15
Live  23
Hotml 09
    The earlier examples were line number ranges and pattern ranges.  sed allows us to use both (line number and pattern) in the same command itself. This command indicates to print the lines from pattern "Redif" till the end of the file($).

8. Similarly, to print contents from the beginning of the file till the pattern "Inbox":
$ sed -n '1,/Inbox/p' file
Gmail 10
Yahoo 20
Redif 18
Inbox 15

No comments:

Post a Comment