O'Reilly logo

Linux and Unix Shell Programming by David Tansley

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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' ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required