Forms of addresses in sed commands
Last revision August 6, 2004
Table of Contents:
|
Addresses can be line numbers. The first line in the first input file is number one. All input files are concatenated (line numbers do not reset for each file). Input line numbers are not altered by text inserted into the output stream or lines deleted from the output. When used as an address, the character $ means "the last line".
Addresses can also be regular expressions that define patterns of characters to match. If the regular expression is found on a line, then that line matches the address. The regular expression must be enclosed within slash characters, as for a pattern search in vi.
If no address is given, a function applies to the entire file.
If one address is given, the function applies only to lines that match that address.
If two addresses are given, separated by a comma (some commands only allow
one), then the function applies to the set of lines consisting of the
line corresponding to the first address and all the lines that follow it
up to and including the line corresponding to the second address.
If the line corresponding to the second address cannot be found in the
file, then the matching set is from the line corresponding to the first
address all the way to the end of the file. For example, the address
pair
10,1000
matches from line number 10 through line number 1000, but if the file
contains less than 1000 lines, it matches from line 10 through the end.
You can give regular expressions as addresses rather than line numbers. In this case, if the second expression does not match any line in the file, then the address pair selects all lines starting from the first line that matches the first expression all the way to the end of the file. Of course, if the first expression does not match any lines in the file, then the pair selects nothing.
Regular expression address pairs may also match multiple sets of lines in
the file. For example, if the address pair is
/^A/,/^$/
then it will match a set of lines that starts with a line that begins with the
capital letter A, and ends with (inclusive) a blank line. But if, after that blank
line, there is another place where a line starts with a capital letter A, then
that line starts a new matching set that is also selected, up to a blank line
(inclusive), or end of file if there is no following blank line. All matching
sets will be selected.