10.6. Writing to a file within sed

As well as using the ‘>’ file redirection to send output to a file, you can send the results from a sed command to a file. The format for this is a little like the write using substitutes. Here’s the format:

[ address[,address]] w filename 

where ‘w’ lets sed know it’s going to write the results to a file, and ‘filename’ is the filename is self-explanatory. It’s straightforward; here are a couple of examples.

						$ sed '1,2 w filedt' quote.txt
					

Here the file is sent to the screen, and the pattern range, which is lines 1 and 2, is sent to the file called filedt.

						$ pg filedt 
The honeysuckle band played all night long for only $90. 
It was an evening of splendid music and company.
					

In this example we search for the ...

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.