10.4. Basic sed editing examples

Let’s see some of sed ’s editing capabilities in action using examples.

10.4.1. Displaying lines using p(rint)

The format for the print command is [address[,address]p. To display a line of text you can supply sed with a line number and sed will display it for you.

							$ sed '2p' quote.txt 
The honeysuckle band played all night long for only $90. 
It was an evening of splendid music and company. 
Too bad the disco floor fell through at 23:10. 
The local nurse Miss P.Neave was in attendance.
						

What went wrong there? We said we wanted line 2 and issued the p to print that line only, but it’s printed all the lines in the file. To fix this we use the -n option to explicitly print addressed (matched) lines.

							$ sed -n '2p' ...

Get Linux and Unix Shell Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.