Skip to Content
sed & awk, 2nd Edition
book

sed & awk, 2nd Edition

by Dale Dougherty, Arnold Robbins
March 1997
Intermediate to advanced
432 pages
11h 31m
English
O'Reilly Media, Inc.
Content preview from sed & awk, 2nd Edition

A Global Perspective on Addressing

One of the first things you’ll notice about sed commands is that sed will apply them to every input line. Sed is implicitly global, unlike ed, ex, or vi. The following substitute command will change every “CA” into “California.”

s/CA/California/g

If the same command were entered from the ex command prompt in vi, it would make the replacement for all occurrences on the current line only. In sed, it is as though each line has a turn at becoming the current line and so the command is applied to every line. Line addresses are used to supply context for, or restrict, an operation. (In short: Nothing gets done in vi unless you tell it which lines to work on, while sed will work on every line unless you tell it not to.) For instance, by supplying the address “Sebastopol” to the previous substitute command, we can limit the replacement of “CA” by “California” to just lines containing “Sebastopol.”

/Sebastopol/s/CA/California/g

An input line consisting of “Sebastopol, CA” would match the address and the substitute command would be applied, changing it to “Sebastopol, California.” A line consisting of “San Francisco, CA” would not be matched and the substitution would not be applied.

A sed command can specify zero, one, or two addresses. An address can be a regular expression describing a pattern, a line number, or a line addressing symbol.

  • If no address is specified, then the command is applied to each line.

  • If there is only one address, the command is applied ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

The AWK Programming Language, 2nd Edition

The AWK Programming Language, 2nd Edition

Alfred V. Aho, Brian W. Kernighan, Peter J. Weinberger

Publisher Resources

ISBN: 1565922255Supplemental ContentErrata Page