Saturday, February 17, 2018

SED Tutorial - How to print a particular line, last line, range of lines from a file












Today let us see the various operations that can be performed using sed for printing or displaying lines from a file. First let us understand the format of print in sed.

We can display the content of file using the line number or a pattern.
“p” is a command for printing the data from the pattern buffer. “-n” is used to suppress automatic printing of pattern space.

I have created a file mytestfile.txt to illustrate the examples. (NOTE : I am executing the commands online in http://www.compileonline.com/execute_bash_online.php)



                                      SED Eg 1 - Example file


How to display a particular line from a file using sed?

We all know that the head and tail command allows us to view from the top or the bottom of the file. But what if we want to view a particular line from middle of the file? This problem can be solved by using the below sed command.
This command will print the ‘N’th line of the file.
For example, if we want to see the 5th line of the file mytestfile.txt, then we use

                                             SED Eg 2 - Print particular line using sed

  How to display a range of lines from a file using sed?


This command will print the lines from M to N.
For example, if we want to view 2 to 5 lines of the file mytestfile.txt, then we use



                                                    SED Eg 3 - Print range of lines using sed



How to display more than one range of lines from a file using sed?


This command will print the lines from M to N and from P to Q. “-e” option means execute.
For example, if we want to view lines from 2 to 5 and from 8 to 9 of the file mytestfile.txt, then we use


                                           SED Eg 4 - Print different range of lines using sed



How to display the entire file except a single line using sed?


This command will delete the line M and print the rest of the file. “-d” option means delete.
For example, if we do want to delete line 4 from of the file mytestfile.txt, then we use



                             SED Eg 5 - Print entire file excluding a single line using sed



How to display the entire file except a range of lines using sed?


This command will delete the lines from M to N and print the rest of the file.
For example, if we do not want to view lines from 2 to 5 of the file mytestfile.txt, then we use


                                       SED Eg 6 - Print entire file excluding range of lines using sed




How to print every Nth line from a particular line using sed?


This command will print every Nth line starting from M line. This command can be used to print odd or even lines in a file.
For example, if we want to view every 2nd line starting from 4th line of the file mytestfile.txt, then we use,

                                         SED Eg 7 - Print Nth line from particular line using sed
The command to view only even lines of file is shown below.

                                           SED Eg 8 - Print only even lines using sed
The command to view only odd lines of file is shown below.



                                         SED Eg 9 - Print only odd lines using sed



How to print the last line of the file using sed?


This command will print the last line of the file.

                                                   SED Eg 10 - Print last line using sed


How to print from Nth line to the last line of a file using sed?


This command will print from Nth line to the last line of the file.
For example, if we want to view from 7th line till the end of the file mytestfile.txt, then we use,

                         SED Eg 11 - Print from Nth line to last line using sed

In the next post, I will show examples of printing lines from a file based on pattern.

0 Responses to “SED Tutorial - How to print a particular line, last line, range of lines from a file”

Post a Comment

Disclaimer

The ideas, thoughts and concepts expressed here are my own. They, in no way reflect those of my employer or any other organization/client that I am associated. The articles presented doesn't imply to any particular organization or client and are meant only for knowledge Sharing purpose. The articles can't be reproduced or copied without the Owner's knowledge or permission.