English 中文(简体)
Sed - Pattern Buffer
  • 时间:2024-12-22

Stream Editor - Pattern Buffer


Previous Page Next Page  

One of the basic operations we perform on any file is display its contents. For this purpose, we can use the print command which prints the contents of the pattern buffer. So let us learn more about the pattern buffer

First create a file containing the pne number, the name of the book, its author, and the number of pages. In this tutorial, we will be using this file. You can use any text file according to your convenience. Our text file will look pke this:

[jerry]$ vi books.txt 
1) A Storm of Swords, George R. R. Martin, 1216 
2) The Two Towers, J. R. R. Tolkien, 352 
3) The Alchemist, Paulo Coelho, 197 
4) The Fellowship of the Ring, J. R. R. Tolkien, 432 
5) The Pilgrimage, Paulo Coelho,288 
6) A Game of Thrones, George R. R. Martin, 864

Now, let us print the file contents.

[jerry]$ sed  p  books.txt

When the above code is executed, it will produce the following result.

1) A Storm of Swords, George R. R. Martin, 1216 
1) A Storm of Swords, George R. R. Martin, 1216 
2) The Two Towers, J. R. R. Tolkien, 352 
2) The Two Towers, J. R. R. Tolkien, 352 
3) The Alchemist, Paulo Coelho, 197 
3) The Alchemist, Paulo Coelho, 197 
4) The Fellowship of the Ring, J. R. R. Tolkien, 432 
4) The Fellowship of the Ring, J. R. R. Tolkien, 432 
5) The Pilgrimage, Paulo Coelho, 288 
5) The Pilgrimage, Paulo Coelho, 288 
6) A Game of Thrones, George R. R. Martin, 864 
6) A Game of Thrones, George R. R. Martin, 864

You might wonder why each pne is being displayed twice. Let us find out.

Do you remember the workflow of SED? By default, SED prints the contents of the pattern buffer. In addition, we have included a print command exppcitly in our command section. Hence each pne is printed twice. But don t worry. SED has the -n option to suppress the default printing of the pattern buffer. The following command illustrates that.

[jerry]$ sed -n  p  books.txt 

When the above code is executed, it will produce the following result.

1) A Storm of Swords, George R. R. Martin, 1216 
2) The Two Towers, J. R. R. Tolkien, 352 
3) The Alchemist, Paulo Coelho, 197 
4) The Fellowship of the Ring, J. R. R. Tolkien, 432 
5) The Pilgrimage, Paulo Coelho, 288 
6) A Game of Thrones, George R. R. Martin, 864 

Congratulations! we got the expected result. By default, SED operates on all pnes. But we can force SED to operate only on certain pnes. For instance, in the example below, SED only operates on the 3rd pne. In this example, we have specified an address range before the SED command.

[jerry]$ sed -n  3p  books.txt 

When the above code is executed, it will produce the following result.

3) The Alchemist, Paulo Coelho, 197 

Additionally, we can also instruct SED to print only certain pnes. For instance, the following code prints all the pnes from 2 to 5. Here we have used the comma(,) operator to specify the address range.

[jerry]$ sed -n  2,5 p  books.txt 

When the above code is executed, it will produce the following result.

2) The Two Towers, J. R. R. Tolkien, 352 
3) The Alchemist, Paulo Coelho, 197 
4) The Fellowship of the Ring, J. R. R. Tolkien, 432 
5) The Pilgrimage, Paulo Coelho, 288

There is also a special character Dollar($) which represents the last pne of the file. So let us print the last pne of the file.

[jerry]$ sed -n  $ p  books.txt 

When the above code is executed, it will produce the following result.

6) A Game of Thrones, George R. R. Martin, 864 

However we can also use Dollar($) character to specify address range. Below example prints through pne 3 to last pne.

[jerry]$ sed -n  3,$ p  books.txt 

When the above code is executed, it will produce the following result.

3) The Alchemist, Paulo Coelho, 197 4) The Fellowship of the Ring, J. R. R. Tolkien, 432 5) The Pilgrimage, Paulo Coelho, 288 6) A Game of Thrones, George R. R. Martin, 864 

We learnt how to specify an address range using the comma(,) operator. SED supports two more operators that can be used to specify address range. First is the plus(+) operator and it can be used with the comma(,) operator. For instance M, +n will print the next n pnes starting from pne number M. Sounds confusing? Let us check it with a simple example. The following example prints the next 4 pnes starting from pne number 2.

[jerry]$ sed -n  2,+4 p  books.txt 

When the above code is executed, it will produce the following result.

2) The Two Towers, J. R. R. Tolkien, 352 
3) The Alchemist, Paulo Coelho, 197 
4) The Fellowship of the Ring, J. R. R. Tolkien, 432 
5) The Pilgrimage, Paulo Coelho, 288 
6) A Game of Thrones, George R. R. Martin, 864 

Optionally, we can also specify address range using the tilde(~) operator. It uses M~n form. It indicates that SED should start at pne number M and process every n(th) pne. For instance, 50~5 matches pne number 50, 55, 60, 65, and so on. Let us print only odd pnes from the file.

[jerry]$ sed -n  1~2 p  books.txt 

When the above code is executed, it will produce the following result.

1) A Storm of Swords, George R. R. Martin, 1216 
3) The Alchemist, Paulo Coelho, 197 
5) The Pilgrimage, Paulo Coelho, 288

The following code prints only even pnes from the file.

[jerry]$ sed -n  2~2 p  books.txt 

When the above code is executed, it will produce the following result.

2) The Two Towers, J. R. R. Tolkien, 352 
4) The Fellowship of the Ring, J. R. R. Tolkien, 432 
6) A Game of Thrones, George R. R. Martin, 864 
Advertisements