Linux Commands

What is P Option in sed?

In UNIX/Linux, the sed (stream editor) command parses and transforms texts with the help of regular expressions. It’s one of the very first tools to support regular expressions. Today, sed is a part of the UNIX specification, thus available on all the modern UNIX/Linux systems by default.

While there are numerous ways to use sed, we will take a look at the P option (with examples) in this guide.

Basic sed Usage

This section will offer a quick refresher of the basic usages of the sed command.

The command structure of sed is as follows:

$ sed <options> <regex_pattern> <stream>

In most cases, sed is used for its substitution function. Have a look at the following example:

$ echo "the quick brown fox" | sed -e 's/quick/QUICK/'

Here, the sed command is operating on the contents of STDOUT from the echo command. Learn more about STDIN, STDERR, and STDOUT in Linux. In the output, the term “quick” is replaced with “QUICK”.

If we wanted to perform this operation on a text file, then the command would look like this:

$ sed -e 's/quick/QUICK/g' sed-demo.txt

Note that the original content of the file wasn’t modified. To modify the file contents in-place, we have to add the “-i” flag. Take a look at the following command:

$ cat sed-demo.txt
$ sed -i -e 's/quick/QUICK/g' sed-demo.txt
$ cat sed-demo.txt

Notice that we also had to add the g option in the expression so that sed looks for pattern matches on the entire stream and performs the task specified.

The P Option in sed

To modify how sed handles various streams, there are numerous options available. We explored the usage of the g option in the context of sed‘s substitution feature. The P option, however, is used for searching a pattern only.

Notice that it’s uppercase P. Lowercase p specifies another option in sed. All these options are to be used within the pattern specification.

  • p: sed will print all the contents in the current pattern space.
  • P: sed will print only the first line of data in the current pattern space.

Remember how sed prints the output of the stream modification on the console by default? With the help of p and P options, we can control what contents sed will print on the console.

For example, to print only the first line, we could use the following technique:

echo “line 1

this is line 2” | sed -n ‘1p’

This could also be achieved using the p option:

echo “line 1

this is line 2” | sed -n ‘1P’

However, we can notice the difference in their behaviors if we include 2 lines in the buffer and print them both:

echo “line 1

this is line 2” | sed -n ‘1{N; P}’

Here, there were two lines in the buffer but we only printed the first one because we’re using the P option.


In this guide, we explored the P option in sed and explored a couple of ways of implementing it. This is just one of the many ways of using sed. This guide demonstrates performing multiple sed replacements. The power of sed originates from its implementation of regular expressions. Find out more about regular expressions in sed.

While sed can find edit text streams, if you need to just search for a specific pattern, then grep is the optimal choice. Besides its basic usage, the grep command supports advanced features like excluding certain items, multiple string patterns, and more. Improve your mastery of the grep command with 30 grep examples.

Happy computing!

About the author

Sidratul Muntaha

Student of CSE. I love Linux and playing with tech and gadgets. I use both Ubuntu and Linux Mint.