Unix Power ToolsUnix Power ToolsSearch this book

34.3. Invoking sed

If you were using sed on the fly, as a stream editor (Section 34.2), you might execute it as simply as this:

% somecommand | sed 's/old/new/' | othercommand

Given filenames, sed will read them instead of standard input:

% sed 's/old/new/' myfile

A simple script can go right on the command line. If you want to execute more than one editing command, you can use the -e option:

% sed -e 's/old/new/' -e '/bad/d' myfile

Or you can use semicolons (;), which are a sed command separator:

% sed 's/old/new/; /bad/d' myfile

Or (especially useful in shell scripts (Section 1.8)) you can use the Bourne shell's ability to understand multiline commands:

sed '
s/old/new/
/bad/d' myfile

Or you can put your commands into a file and tell sed to read that file with the -f option:

% sed -f scriptfile myfile

There's only one other command-line option: -n. sed normally prints every line of its input (except those that have been deleted by the editing script). But there are times when you want only lines that your script has affected or that you explicitly ask for with the p command. In these cases, use -n to suppress the normal output.

-- TOR



Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.