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!

Command Line Kung Fu

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

Command Line Kung Fu is packed with dozens of tips and practical real-world examples. You won't find theoretical examples in this book. The examples demonstrate how to solve actual problems. The tactics are easy to find, too. Each chapter covers a specific topic and groups related tips and examples together.

$ Check price
14.9914.99Amazon 4.5 logo(27+ reviews)

More Linux & UNIX resources

Aching back from coding all day?

Foam Seat Cushion

Aching back from coding all day? Try Back Problems

This foam seat cushion relieves lowerback pain, numbness and pressure sores by promoting healthy weight distribution, posture and spine alignment. Furthermore, it reduces pressure on the tailbone and hip bones while sitting. Perfect for sitting on the computer desk for long periods of time.

$ Check price
99.9599.95Amazon 4.5 logo(9,445+ reviews)

More Back Problems resources

Ad