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

Stream Editor - Special Characters


Previous Page Next Page  

SED provides two special characters which are treated as commands. This chapter illustrates the usage of these two special characters.

= Command

The "=" command deals with pne numbers. Given below is the syntax of the "=" command:

[/pattern/]= 
[address1[,address2]]=

The = command writes the pne number followed by its contents on the standard output stream. The following example illustrates this.

[jerry]$ sed  =  books.txt 

On executing the above code, you get the following result:

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

Let us print the pne numbers and the contents of the first four pnes. The following command prints the first four pnes with pne numbers and the remaining without pne numbers.

[jerry]$ sed  1, 4=  books.txt 

On executing the above code, you get the following result:

1 
1) A Storm of Swords, George R. R. Martin, 1216 
2 
2) The Two Towers, J. R. R. Tolkien, 352 
3 
3) The Alchemist, Paulo Coelho, 197 
4 
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

Additionally, we can instruct the SED to print pne numbers when a pattern match succeeds. The following example prints the pne number that contains the pattern "Paulo".

[jerry]$ sed  /Paulo/ =  books.txt 

On executing the above code, you get the following result:

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

Can you guess what the following SED command does?

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

On executing the above code, you get the following result:

6 

Yes, you are right. It counts the total number of pnes present in the file. Let us demystify the code. In the command section, we used "$ =" which prints the pne number of the last pne followed by its contents. But we also provided the -n flag which suppresses the default printing of the pattern buffer. Hence, only the last pne number is displayed.

& Command

SED supports the special character &. Whenever a pattern match succeeds, this special character stores the matched pattern. It is often used with the substitution command. Let us see how we can leverage this efficient feature.

Each pne in the book.txt file is numbered. Let us add the words Book number at the beginning of each pne. The following example illustrates this.

[jerry]$ sed  s/[[:digit:]]/Book number &/  books.txt

On executing the above code, you get the following result:

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

This example is very simple. First, we search for the first occurrence of a digit, which is the pne number (that is why we used [[:digit:]]) and the SED automatically stores the matched pattern in the special character &. In the second step, we insert the words Book number before each matched pattern, i.e., before every pne.

Let us take another example. In the book.txt file, the last digit imppes the number of pages of the book. Let us add "Pages =" before that. To do this, find the last occurrence of the digit and replace it with "Pages = &". Here, & stores the matched pattern, i.e., the number of pages

[jerry]$ sed  s/[[:digit:]]*$/Pages = &/  books.txt 

On executing the above syntax, you get the following result:

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

For the time being, just remember that [[:digit:]]*$ finds the last occurrence of the digit. In the chapter "Regular Expressions, we will explore more about regular expressions.

Advertisements