Using sed
There are two ways to invoke sed: either you specify your editing instructions on the command line or you put them in a file and supply the name of the file.
Specifying Simple Instructions
You can specify simple editing commands on the command line.
sed [-e] 'instruction
'file
The -e
option is necessary only when you supply
more than one instruction on the command line. It tells sed to
interpret the next argument as an instruction. When there is a single
instruction, sed is able to make that determination on its own. Let’s
look at some examples.
Using the sample input file, list, the following example uses the s command for substitution to replace “MA” with “Massachusetts.”
$ sed 's/MA/Massachusetts/' list
John Daggett, 341 King Road, Plymouth Massachusetts
Alice Ford, 22 East Broadway, Richmond VA
Orville Thomas, 11345 Oak Bridge Road, Tulsa OK
Terry Kalkas, 402 Lans Road, Beaver Falls PA
Eric Adams, 20 Post Road, Sudbury Massachusetts
Hubert Sims, 328A Brook Road, Roanoke VA
Amy Wilde, 334 Bayshore Pkwy, Mountain View CA
Sal Carpenter, 73 6th Street, Boston Massachusetts
Three lines are affected by the instruction but all lines are displayed.
Enclosing the instruction in single quotes is not required in all cases but you should get in the habit of always doing it. The enclosing single quotes prevent the shell from interpreting special characters or spaces found in the editing instruction. (The shell uses spaces to determine individual arguments submitted to a program; ...
Get sed & awk, 2nd Edition 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.