Introduction to Linux - A Hands on Guide | Linux Bible | Linux From Scratch | A Newbie's Getting Started Guide to Linux | Linux Command Line Cheat Sheet | More Linux eBooks



Sunday, 12 April 2015

Sed Command in Linux - Print Lines in a File

This is the first article of the "Super sed' Series", in which we will learn how to print lines in a file using line numbers and regular expressions.


Before we directly jump to the main content, every learner should know what sed is. Here is the brief introduction of the Super sed:

  • sed stand for Stream EDitor and it being based on the ed editor, it borrows most of the commands from the ed. It was developed by Lee E. McMahon of Bell Labs.
  • sed offers large range of text transformations that include printing lines, deleting lines, editing line in-place, search and replace, appending and inserting lines, etc.
  • sed is useful whenever you need to perform common editing operations on multiple lines without using 'vi' editor.
  • Whenever sed is executed on an input file or on the contents from stdin, sed reads the file line-by-line and after removing the trailing newline, places it in the "Pattern space", where the commands are executed on them after conditions (as in case of regex matching) are verified, and then printed on the stdout.

Printing Lines from a File using sed

Before we start, just remember two points:
  1. sed "p" command lets us print specific lines based on the line number or regex provided.
  2. sed with option -n will suppress automatic printing of pattern buffer/space. So, we would want to use this option. (Explained in later section)
For our better understanding, let us have a file sedtest.txt with contents as follows:

$ cat sedtest.txt
This is line #1
This is line #2
This is line #3
This is line #4
This is line #5
This is line #6
This is line #7
This is line #8
This is line #9
This is line #10

A. sed - Print Lines with Line Number

1. Print 'N'th line

This will print 'N'th line in the FILE.txt.

Syntax:

sed -n 'Np' FILE.txt
Example:
To print 1st line,

$ sed -n '1p' sedtest.txt
This is line #1
While, to print last line,

$ sed -n '$p' sedtest.txt
This is line #10
Let us see, what if we had not used option -n, what would have been the result.

$ sed '5p' sedtest.txt
This is line #1
This is line #2
This is line #3
This is line #4
This is line #5
This is line #5
This is line #6
This is line #7
This is line #8
This is line #9
This is line #10
You can easily see that 5th line is printed twice, one from pattern buffer/space and other is the result of sed '5p'.

2. Print all Lines starting from 'M'th up to 'N'th

This will print the block of lines starting at line number M and ending at line number N.

Syntax:

sed -n 'M,Np' FILE.txt
Example:
To print 3rd line to 8th line.

$ sed -n '3,8p' sedtest.txt
This is line #3
This is line #4
This is line #5
This is line #6
This is line #7
This is line #8
Similarly, in order to print lines starting from 5th up to last line, you would run-

$ sed -n '5,$p' sedtest.txt
This is line #5
This is line #6
This is line #7
This is line #8
This is line #9
This is line #10

3. Print Every 'N'th Line Starting from 'M'th Line

This will print Mth line and every Nth line coming after that.

Syntax:

sed -n 'M~Np' FILE.txt
Example:
To print every alternate line staring from 2nd one.

sed -n '2~2p' sedtest.txt
This is line #2
This is line #4
This is line #6
This is line #8
This is line #10

B. sed - Print Lines with Regular Expression/Pattern

1. Print lines containing a Pattern

This will print the line that contains the pattern provided.

Syntax:

sed -n '/PATTERN/p' FILE.txt
Example:

$ sed -n '/4/p' sedtest.txt
This is line #4

2. Print lines excluding a Pattern

This will print all those lines which do not contain the pattern provided.

Syntax:

sed -n '/PATTERN/!p' FILE.txt
Example:

$ sed -n '/4/!p' sedtest.txt
This is line #1
This is line #2
This is line #3
This is line #5
This is line #6
This is line #7
This is line #8
This is line #9
This is line #10

3. Print Block of lines starting from pattern matching line

This will start printing the lines from the one where pattern matches, till 'N'th line.

Syntax:

sed -n '/PATTERN/,Np' FILE.txt
Example:

$ sed -n '/5/,8p' sedtest.txt
This is line #5
This is line #6
This is line #7
This is line #8
Of course, in order to print all the lines starting from pattern matching line till the end, you would use '/PATTERN/,$p' as follows -

$ sed -n '/5/,$p' sedtest.txt
This is line #5
This is line #6
This is line #7
This is line #8
This is line #9
This is line #10

4. Print Block of lines ending at pattern matching line

This will start printing the lines starting from the 'N'th line, till the one where pattern matches.

Syntax:

sed -n 'N,/PATTERN/p' FILE.txt
Example:

$ sed -n '4,/9/p' sedtest.txt
This is line #4
This is line #5
This is line #6
This is line #7
This is line #8
This is line #9
So, here, if you wish to start from first line and end at a line matching a pattern, you would use -

$ sed -n '1,/6/p' sedtest.txt
This is line #1
This is line #2
This is line #3
This is line #4
This is line #5
This is line #6

5. Print Block of lines between two Pattern matches

This will start printing lines from 1st pattern match till 2nd pattern match.

Syntax:

sed -n '/PATTERN1/,/PATTERN2/p' FILE.txt
Example:

$ sed -n '/5/,/8/p' sedtest.txt
This is line #5
This is line #6
This is line #7
This is line #8
    That was all about the first article on sed command. More articles on sed are coming soon. So, stay tuned. Of course, do not forget to share your feedback in the comment section below.

1 comment: