Print Lines Using Sed p Command: Comprehensive Guide

The p command in sed is used to print the current pattern space, which is the current line of text.

You can use the sed command with the p option to print lines of text from a file like this:

sed -n 'p' filename

In this tutorial, you will learn about the various ways to use the sed p command to print lines from a file or a stream of data.

 

 

Printing Specific Line

You can use sed to print a specific line from a file. For example, if you want to print the 5th line from a file, you can use the following command:

sed -n '5p' filename

In this command, -n suppresses automatic printing of the pattern space and 5p instructs sed to print the 5th line of the file.

 

Printing Multiple Lines

To print a range of lines from a file, use the following syntax:

sed -n 'X,Yp' filename

Where X is the start line and Y is the end line.

For example, to print lines 10 to 20 from a file, use the following command:

sed -n '10,20p' filename

This command will print lines 10 to 20 from the file.

 

Suppressing Automatic Printing of Pattern Space

By default, sed prints every line of the input file to the terminal.

The -n option suppresses the automatic printing of the pattern space, meaning sed will not print anything unless explicitly told to do so.

For example, consider a file called example.txt with the following content:

1. This is line 1
2. This is line 2
3. This is line 3
4. This is line 4
5. This is line 5

If you run the following command:

sed 'p' example.txt

The output will be:

1. This is line 1
1. This is line 1
2. This is line 2
2. This is line 2
3. This is line 3
3. This is line 3
4. This is line 4
4. This is line 4
5. This is line 5
5. This is line 5

Every line is printed twice because sed automatically prints the pattern space and the p command also prints the pattern space.

To suppress the automatic printing of the pattern space, use the -n option:

sed -n 'p' example.txt

Now, the output will be:

1. This is line 1
2. This is line 2
3. This is line 3
4. This is line 4
5. This is line 5

 

Printing Lines Matching a Pattern

If you want to print all lines from a file that contains a specific word, you can use the following command:

sed -n '/error/p' filename

In this command, /error/p instructs sed to print only the lines that contain the word “error”.

Consider a file called logs.txt with the following content:

INFO: Starting the application
ERROR: Failed to start the application
INFO: Retrying to start the application
ERROR: Failed to start the application again
INFO: Application started successfully

If you run the following command:

sed -n '/ERROR/p' logs.txt

The output will be:

ERROR: Failed to start the application
ERROR: Failed to start the application again

Only the lines containing the word “ERROR” are printed.

 

Printing Lines Not Matching a Pattern

You can use the ! symbol before the p command in sed to print lines that do not match a specific pattern

sed -n '/error/!p' filename

In this command, /error/!p instructs sed to print only the lines that do not contain the word “error”.

Using the same logs.txt file from the previous example, if you run the following command:

sed -n '/ERROR/!p' logs.txt

The output will be:

INFO: Starting the application
INFO: Retrying to start the application
INFO: Application started successfully

Only the lines not containing the word “ERROR” are printed.

 

Printing Lines Before/After Matching a Pattern

With sed, you can also print lines before or after a line that matches a specific pattern.

Printing Lines Before a Match

To print lines before a line that matches a specific pattern, you can use the following syntax:

sed -n '/pattern/{x;p;d;}; x' filename

For example, to print 1 line before a line that matches the word “ERROR”, you can use the following command:

sed -n '/successfully/{x;p;d;}; x' logs.txt

Using the logs.txt file from the previous examples, this command will output:

ERROR: Failed to start the application again

Printing Lines After a Match

To print lines after a line that matches a specific pattern, you can use the following syntax:

sed -n '/pattern/{n;p;q;}' filename

For example, to print 1 line after a line that matches the word “ERROR”, you can use the following command:

sed -n '/ERROR/{n;p;q;}' logs.txt

Using the logs.txt file from the previous examples, this command will output:

INFO: Retrying to start the application
INFO: Application started successfully

 

Printing Until a Pattern

You can use sed to print all lines from the start of the file until a line that matches a specific pattern.

The syntax for this is as follows:

sed -n '/pattern/q;p' filename

For example, to print all lines from a file until the line containing the word “stop”, you can use the following command:

sed -n '/stop/q;p' filename

/stop/q – This part of the command tells sed to quit (q) when it encounters a line containing the pattern “stop”.

Consider a file called content.txt with the following content:

This is the first line.
This is the second line.
stop
This is the fourth line.
This is the fifth line.

If you run the following command:

sed -n '/stop/q;p' content.txt

The output will be:

This is the first line.
This is the second line.

All lines from the start of the file until the line containing the word “stop” are printed.

 

Printing Lines Between Two Patterns

You can use sed to print lines that are between two specified patterns. This can be useful for extracting a specific block of text from a file.

The syntax for this is as follows:

sed -n '/pattern1/,/pattern2/p' filename

Where pattern1 is the starting pattern and pattern2 is the ending pattern.

For example, to print all lines from a file that are between the lines containing “Start” and “End”, you can use the following command:

sed -n '/Start/,/End/p' filename

Consider a file called section.txt with the following content:

This is some text.
Start
This is the section you want to extract.
It contains multiple lines.
End
This is some more text.

If you run the following command:

sed -n '/Start/,/End/p' section.txt

The output will be:

Start
This is the section you want to extract.
It contains multiple lines.
End

All lines between the lines containing “Start” and “End” are printed including them.

To exclude the “Start” and “End”, you can use the delete command (d) like this:

sed -n '/Start/,/End/ { /Start/d; /End/d; p }' section.txt

The output will be:

This is the section you want to extract.
It contains multiple lines.

 

 

Printing Specific Lines

The syntax for printing specific line numbers is as follows:

sed -n 'Xp;Yp' filename

Where X and Y are the line numbers you want to print.

For example, to print the 3rd and 5th lines from a file, you can use the following command:

sed -n '3p;5p' filename

Consider a file called data.txt with the following content:

1. Apple
2. Banana
3. Cherry
4. Date
5. Elderberry

If you run the following command:

sed -n '3p;5p' data.txt

The output will be:

3. Cherry
5. Elderberry

Only the 3rd and 5th lines are printed.

 

Printing Lines Starting From a Specific Line To End

You can use sed to print all lines starting from a specific line number till the end of the file like this:

sed -n 'X,$p' filename

Where X is the starting line number.

For example, to print all lines starting from the 3rd line, you can use the following command:

sed -n '3,$p' filename

Using the data.txt file from the previous example:

sed -n '3,$p' data.txt

The output will be:

3. Cherry
4. Date
5. Elderberry

All lines from the 3rd line till the end of the file are printed.

 

Printing Lines Matching a Regular Expression

The syntax for printing lines that match a regular expression is as follows:

sed -n '/regex/p' filename

For example, to print all lines from a file that start with the letter “A”, you can use the following command:

sed -n '/^A/p' filename

In this command, ^A is a regular expression that matches any line that starts with the letter “A”.

Consider a file called fruits.txt with the following content:

Apple
Banana
Apricot
Date
Avocado

If you run the following command:

sed -n '/^A/p' fruits.txt

The output will be:

Apple
Apricot
Avocado

Only the lines that start with the letter “A” are printed.

Printing Lines Not Matching a Regular Expression

You can also use sed to print lines that do not match a specific regular expression like this:

sed -n '/regex/!p' filename

For example, to print all lines from a file that do not start with the letter “A”, you can use the following command:

sed -n '/^A/!p' filename

In this command, ^A is a regular expression that matches any line that starts with the letter “A”.

Using the fruits.txt file from the previous example, if you run the following command:

sed -n '/^A/!p' fruits.txt

The output will be:

Banana
Date

Only the lines that do not start with the letter “A” are printed.

 

Printing Lines Containing Special Characters

When dealing with special characters like *, ., /, \, &, |, [, ], {, }, (, ), ^, and $, you must escape them with a backslash \ in order to match them literally.

The syntax for printing lines that contain a special character is as follows:

sed -n '/\character/p' filename

For example, to print all lines from a file that contain the * character, you can use the following command:

sed -n '/\*/p' filename

Consider a file called special.txt with the following content:

This is a test.
This line contains * character.
This is another test.

If you run the following command:

sed -n '/\*/p' special.txt

The output will be:

This line contains * character.

Only the line containing the * character is printed.

 

Modifying Before Printing

sed allows you to modify lines while printing them using a combination of the p command with other commands like s (substitute).

Combining (p) with substitution (s) command

The s command in sed is used for substitution. When you combine p with s, sed will substitute the specified text and then print the line.

The syntax for this is as follows:

sed -n 's/pattern/replacement/p' filename

For example, to replace the word “test” with “exam” and then print the line, you can use the following command:

sed -n 's/test/exam/p' filename

Using the test.txt file from the previous example, if you run the following command:

sed -n 's/test/exam/p' test.txt

The output will be:

This is a exam.
This is another exam.

The word “test” is replaced with “exam” and the lines are printed.

 

Print Multiple Input Files

When you provide multiple input files, sed will process them one after the other in the order they are listed.

The syntax for using the sed p command with multiple input files is as follows:

sed -n 'p' file1 file2 ... fileN

For example, to print the 3rd line from two files, data1.txt and data2.txt, you can use the following command:

sed -n '3p' data1.txt data2.txt

Consider two files, data1.txt and data2.txt, with the following content:

data1.txt:

1. Apple
2. Banana
3. Cherry

data2.txt:

4. Date
5. Elderberry
6. Fig

If you run the following command:

sed -n '3p' data1.txt data2.txt

The output will be:

3. Cherry
6. Fig

The 3rd line from each file is printed.

Leave a Reply

Your email address will not be published. Required fields are marked *