02. Line Addresses and Printing

Invoking the sed command without any line address will cause sed to automatically perform operations on all lines.

Printing

Firstly, we can print out text through the sed command with the p option. This command simply outputs the file or whatever is passed through standard in.

$ sed 'p' oneOS.txt
One OS to rule them all, One OS to rule them all, One OS to find them. One OS to find them. One OS to call them all, One OS to call them all, And in salvation bind them. And in salvation bind them. In the bright land of Linux, In the bright land of Linux, Where the hackers play. Where the hackers play.

Now here, we can see that our text file prints out twice. This is because sed is reading in a line, performing the "print" operation on it, then outputting it again. To suppress the automatic printing, we use the -n option.

$ sed -n 'p' oneOS.txt
One OS to rule them all, One OS to find them. One OS to call them all, And in salvation bind them. In the bright land of Linux, Where the hackers play.

Great! Keep the -n option in mind, as we'll use it later.

If we want to be more specific, we can precede the option with a line address.

Specifying address ranges

Now let's see how we can specify specific lines to print out. There are a handful of ways to specify a range or a specific pattern.

n
Operate only on line n.
'3p' would print just the third line.
n,m
Print lines n to m (inclusive).
'3,5p' would print the 3rd, 4th, and 5th lines.
,m
Print lines from beginning of file to line m (inclusive).
n,+m
Print m startings from n (GNU only).
'3,+2p' would print lines 3 to 5, inclusive.
May not be supported in some versions.
n~m
Starting from line n, print every other m line (GNU only).
'0~2' would print even numbered line.
May not be supported in some versions.
$
Last line.
'$p' would print just the last line.
!
Proceed the line address with an exclamation point to specific all lines except these addresses.
'1,2!p' would print all lines except the first and second.

In our example:

$ sed -n '2,3p' oneOS.txt
One OS to find them. One OS to call them all,
$ sed -n '$p' oneOS.txt
Where the hackers play.

Line addresses with regular expressions

You may also specify lines that match a particular regular expression. Simply embed the regex between two slashes before the option.

This command prints only lines that start with "One".

$ sed -n '/^One/p' oneOS.txt
One OS to rule them all, One OS to fine them. One OS to call them all,

This next command prints only lines that do not contain "OS" in them.

$ sed -n '/OS/!p' oneOS.txt
And in salvation bind them. In the bright land of Linux, Where the hackers play.

Printing sections between two patterns

We can also pull out text between two regex patterns.

$ sed -n '/call/,/hackers/p' oneOS.txt
One OS to call them all, And in salvation bind them. In the bright land of Linux, Where the hackers play.

Now that we are able to select which lines we want, let's move on to how to delete lines.

Take your Linux skills to the next level!

System Admin Handbook

Take your Linux skills to the next level! Try Linux & UNIX

This book approaches system administration in a practical way and is an invaluable reference for both new administrators and experienced professionals. It details best practices for every facet of system administration, including storage management, network design and administration, email, web hosting, scripting, and much more.

$ Check price
74.9974.99Amazon 4.5 logo(142+ reviews)

More Linux & UNIX resources

Aching back from coding all day?

Prism Glasses

Aching back from coding all day? Try Back Problems

Ever feel achy from sitting crunched up on your computer table? Try lying down with these optical glasses that allow you to work on your laptop while lying flat on your back. This is the perfect solution with those with limited mobility or those who wish to prevent neck cramps and back strains.

$ Check price
4.454.45Amazon 4 logo(128+ reviews)

More Back Problems resources

Ad