Addressing lines
By default, sed attempts to apply each command it has been given
to each and every line of the data file.
Whether this is successful depends on the command and its target.
But sed also provide an addressing mechanism to preemptively
choose or exclude lines to apply commands to.
address sed-command
Addresses are placed at the beginning of the sed command line.
Literal lines
User may specify a specific line to apply the command to.
1 s/\<U\.S\./United States/
Because the end of file (last line) is generally not known until it
is encountered, the $ can be used to represent the last line in a data file.
$ s/\<The End\>/&, But not really./
Range of lines
5,20 s/\<up\>/down/g
Note comma between start and stop range.
Edit command will be applied to both the start and stop lines and all lines
in between.
If multiple ranges desired, then create separate command statements with
the appropriate ranges.
% - the percent sign may be used to specify all lines.
In sed, this may not be meaningful, but in vi, it can be very
useful.
Regular expressions to match line
To use a regular expression to match a line,
specify the regular expression statement in / /
at the beginning of the line.
The / / are required and cannot be substituted.
/^Totals: / s/\<periodically\>/weekly/g
/^Totals: / s#\<periodically\>#weekly#g
If line starts with Totals: then substitute all occurrence of
periodically with weekly on that line.
Like literal line numbers, regular expressions can be used to specify ranges.
/^<pre>/,/^<\/pre>/ s/^/ /
This will indent any lines in a block delimited by <pre> and </pre>
at the start of the line.
Like the line numbers, the two lines with the delimiters will be edited.
When using paired regular expressions, you may find that edit command can
be turned on and off several times when processing a data file.
Note : in vi only the 1st range is processed. Command must be repeated.
Not - invert match results.
1 !s/President/Mr./g
On all lines except the 1st line, change all occurrences of President to Mr.
When used with an address, the NOT indicates to NOT apply the
command when the address or range match.
The NOT is affixed to the command.
5,10 !s/^/ /
This will indent lines 1 through 4 and 11 on.
{} - braces
Apply multiple edit actions based on a single test condition.
start-condition,stop-condition {
edit-cmd1
edit-cmd2
}
Note, the open brace, {, must appear as last character on the filter
line.
The edit commands inside the braces may have their own address filter.
The edit commands inside the braces may have their own brace grouping.
start-condition,stop-condition {
start-condition ! {
stop-condition ! {
edit-cmd1
}
}
edit-cmd2
}
The code above applies edit-cmd1 to all lines in the block except the line
that matched the start-condition and the line that matched the stop-condition.
And it will apply edit-cmd2 to all lines including the start and stop
conditions.
Alternative format :
The following is legal but not the easiest to read.
/^The/ { s/a/TMP123/g; s/the/a/g; s/TMP123/the/g; }