Linux / Unix: sed Command Print Only Matching Lines

I see many examples and man pages on how to do operations like search-and-replace using sed. But, I want to match a given string or a regular pattern expression and display on the screen. How do I print lines with matching pattern using sed command only under Unix like operating systems?

Tutorial details
Difficulty level Easy
Root privileges No
Requirements sed
Est. reading time N/A
Sed is a stream editor for Unix like operating systems. It can perform basic text transformations on:
  • An input stream.
  • A file (regular text file).
  • Input from a pipeline (command1 | sed -e ‘expr1’ | command2).

Sed printing syntax and examples

Use the following syntax:

sed '/word/p' input
sed '/regexp/p' input
sed -n '/regex/p' /path/to/file
sed -n '/regexp/p' /path/to/file | less
sed -n -e '/regexp/p' input > output
## gnu/sed - update file with -i option ###
sed -i -n -e '/regexp/p' input

In this example print or show any matching lines from /etc/passwd file:

sed -n '/root/p' /etc/passwd

Sample outputs:

root:x:0:0:root:/root:/usr/local/sbin/nologin_corp

The -n option asks sed to not display the regex or patten unless explicitly asked to do so. Try removing -n option and you will understand the difference:

sed '/root/p' /etc/passwd

You can print lines using line addresses. In this example print second line:

sed '2p' /etc/passwd

The ‘p’ command is preceded by a ‘2’. The number ‘2’ refers to line number two. You can tell sed to perform prints only on a particular line or lines. In this example print 1 to 5 lines:

sed '1,5p' /etc/passwd

The ‘p’ command is preceded by line rage address 1,5 (separated by a comma). You can also print using two regexps and the syntax is:

sed -n -e '/WORD-1/,/WORD-2/p' input.file
sed -n -e '/regexpA/,/regexpB/p' input.file

In this example, print a block of data that starts with a line containing “BROWN”, and ending with a line that contains “GREEN”:

sed -n -e '/BROWN/,/GREEN/p' colours.txt

The following example will print section of file from a line contcontaining “GREEN” to end of file:

sed -n '/GREEN/,$p' colours.txt
Recommened readings

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 3 comments so far... add one


CategoryList of Unix and Linux commands
Disk space analyzersdf duf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Modern utilitiesbat exa
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg glances gtop jobs killall kill pidof pstree pwdx time vtop
Searchingag grep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
3 comments… add one
  • chandan May 14, 2015 @ 9:49

    I am searching print a line number when specific word found.Could you please help me?

    • Peter J Nelson Jul 15, 2015 @ 18:19

      chandan:

      use “grep -n ” to print line numbers with matches. Add a “-i” option to ignore case.

  • tabassum Sep 5, 2015 @ 6:27

    I have two files containing list of some proteins and another containing interactions of proteins including file 1 proteins with their interacting partners. I want to fetch all the interactions of file 1 proteins but among them only, discarding any new protein.
    What command or script I should use??

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum