We perform basic operations on any file and display its contents. In order to achieve this, we can print a command with the print mode buffer. This tutorial will cover more pattern buffers and how to print the contents of a file that uses different operators for the relevant pattern buffers.
Consider that we have a text file Books.txt to be processed, it has the following content:
1) A Storm of Swords, George R. R. Martin, 1216
2) The Towers, J. R. R. Tolkien, 352
3) The Alchemist, Paulo C Oelho, 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
Sed P Command
We can use the SED ' P ' command to print the contents of the specified file. The following is a simple command to print the contents of a file books.txt.
[jerry]$ sed ' P ' books.txt
When the above code is executed, the following result is produced.
1) A storm of Swords, George R. R. Martin, 1216
1) A storm of Swords, George R. R. Martin, 1216
2) The both Towers, J. R. R. Tolkien, 352
2) The Towers, J. R. R. Tolkien, 352
3) The Alchemist, Paulo Coelho, 197
3) The ALC Hemist, 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
Let's find out why each line is printed two times. In fact, by default, the contents of the SED print mode buffer. In addition, we have explicitly accessed the command part of the Print command. Therefore, each line is printed two times. SED has a-n option to suppress the default printing of the mode buffer. Let's try the following command:
When the above code is executed, the following result is produced.
1) A Storm of Swords, George R. R. Martin, 1216
2) The Towers, J. R. R. Tolkien, 352
3) The Alchemist, Paulo C Oelho, 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
Sed address range
By default, SED operates on all lines. However, you can force sed to be used only on certain lines. For example, in the example below, sed runs only on the third row. In this example, we specify the previous address range of the SED command.
When the above code is executed, the following result is produced.
3) The Alchemist, Paulo Coelho, 197
Sed comma Command
The following code prints 2〜5. Here we use all the lines in the range of addresses specified by the comma (,) operator:
When the above code is executed, the following result is produced.
2) The 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
Sed $ operator
We can use the dollar sign $ operator to print the last line of the file as follows:
When the above code is executed, the following result is produced.
6) A Game of Thrones, George R. R. Martin, 864
However, we can also use the dollar sign ($) to specify an address range. The following example prints through line 3rd to the last line.
When the above code is executed, the following result is produced.
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
Sed plus Operation
The plus (+) operator of SED can be used with the comma (,) operator. For example m,+ n will print the number of lines m, the following example begins printing from line 2nd to the next 4 lines:
When the above code is executed, the following result is produced.
2) The 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
Sed Wave line operator
Alternatively, we can also use the range of addresses specified by the wave symbol (~) operator. It takes the form of m〜n. This indicates that SED should start the line number m and process every n (th) row. For example, 50〜5 line number 50,55,60,65, and so on. Let's have only odd lines from the printed file.
When the above code is executed, the following result is produced.
1) A Storm of Swords, George R. R. Martin, 1216
3) The Alchemist, Paulo Coelho, 197
5) The Pilgrimage, Paulo Coelh O, 288
The following code prints only even rows of files.
When the above code is executed, the following result is produced.
2) The Towers, J. R. R. Tolkien, 352
4) The Fellowship of the Ring, J. R. R. Tolkien, 432
This tutorial describes how SED handles a pattern range. The pattern range can be a simple text or a complex regular expression. We will start using the following text file Books.txt:
1) A Storm of Swords, George R. R. Martin, 1216
2) The Towers, J. R. R. Tolkien, 352
3) The Alchemist, Paulo C Oelho, 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
The following example prints the author of all books Paulo Coelho
[jerry]$ sed-n '/paulo/p ' books.txt
Executing the above code, you will get the following result:
3) The Alchemist, Paulo Coelho, 197
5) The Pilgrimage, Paulo Coelho, 288
Sed typically runs on each line and prints only those rows that meet the given criteria for usage patterns.
We can also put a pattern range, address range. The following example prints the start line with the first line of alchemist matching until the fifth line.
[jerry]$ sed-n '/alchemist/, 5 p ' books.txt
Executing the above code, you will get 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
You can use the dollar sign ($) to discover all the lines that were printed after the first occurrence of the pattern. The following example finds the first occurrence of the string fellowship and prints the remaining lines in the file immediately
[jerry]$ sed-n '/the/,$ p ' books.txt
Executing the above code, you will get the following result:
4) The Fellowship of the Ring, J. R. R. Tolkien, 432
5) The Pilgrimage, Paulo Coelho, 288
6) A Game of Thrones, Ge Orge R. R. Martin, 864
You can also specify multiple pattern ranges using the comma (,) operator. The following example prints the rows that exist between all modes two and pilgrimage.
Executing the above code, you will get the following result:
2) The 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
In addition, we can also use the plus (+) operation in the pattern range. The following example finds the first occurrence of pattern two and prints the next 4 lines after it.
[jerry]$ sed-n '/two/, +4 p ' books.txt
Executing the above code, you will get the following result:
2) The 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
Here, only a few examples are given to familiarize yourself with SED. Try to write a couple of examples by combining the examples above.
From:http://www.yiibai.com/sed/sed_useful_recipes.html