sed print

To use the sed print command you need the 'p' option

sed 'p' /etc/passwd

Will print all the lines of /etc/passwd twice

sed -n 'p' /etc/passwd

-n suppresses the standard output, so only the matching lines will be displayed

If you need to print only the first three lines

sed -n '1,3 p' /etc/passwd

root:x:0:0:root:/root:/bin/bash daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin bin:x:2:2:bin:/bin:/usr/sbin/nologin

If you need to print only the lines containing a certain pattern

For example, those starting with root

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

root:x:0:0:root:/root:/bin/bash

Let's look at the file sites.txt

less sites.txt

AndreyOlegovich.ru IT
Aviasales Travel
Booking.com Hotels
Hotellook Hotels
TopBicycle.ru Bicycles
Velodrive Bicycles
HeiHei.ru Travel
Xiaomi Android
Samsung Android
Book24 Books
GeekBrains Education
Netology Education
TestSetup.ru Education
Pluralsight Education
SovcomInsurance Insurance
Polis 812 Insurance
Polis 812 Insurance
Vivo Android
Reg.ru Hosting
OLDI Laptops
S7 Travel

Let's print the lines from the ninth to the last one

sed -n '9,$ p' sites.txt

Samsung Android
Book24 Books
GeekBrains Education
Netology Education
TestSetup.ru Education
Pluralsight Education
SovcomInsurance Insurance
Polis 812 Insurance
Polis 812 Insurance
Vivo Android
Reg.ru Hosting
OLDI Laptops
S7 Travel

Let's print the lines starting with S

sed -n '/^S/ p' sites.txt

TestSetup.ru Education
Samsung Android
S7 Travel

Let's print the lines starting with S and digits

sed -n '/^S[0-9]/ p' sites.txt

S7 Travel

Article author: Andrei Olegovich