sed: Addresses overview
1
1 4.1 Addresses overview
1 ======================
1
1 Addresses determine on which line(s) the 'sed' command will be executed.
1 The following command replaces the word 'hello' with 'world' only on
1 line 144:
1
1 sed '144s/hello/world/' input.txt > output.txt
1
1 If no addresses are given, the command is performed on all lines.
1 The following command replaces the word 'hello' with 'world' on all
1 lines in the input file:
1
1 sed 's/hello/world/' input.txt > output.txt
1
1 Addresses can contain regular expressions to match lines based on
1 content instead of line numbers. The following command replaces the
1 word 'hello' with 'world' only in lines containing the word 'apple':
1
1 sed '/apple/s/hello/world/' input.txt > output.txt
1
1 An address range is specified with two addresses separated by a comma
1 (','). Addresses can be numeric, regular expressions, or a mix of both.
1 The following command replaces the word 'hello' with 'world' only in
1 lines 4 to 17 (inclusive):
1
1 sed '4,17s/hello/world/' input.txt > output.txt
1
1 Appending the '!' character to the end of an address specification
1 (before the command letter) negates the sense of the match. That is, if
1 the '!' character follows an address or an address range, then only
1 lines which do _not_ match the addresses will be selected. The
1 following command replaces the word 'hello' with 'world' only in lines
1 _not_ containing the word 'apple':
1
1 sed '/apple/!s/hello/world/' input.txt > output.txt
1
1 The following command replaces the word 'hello' with 'world' only in
1 lines 1 to 3 and 18 till the last line of the input file (i.e.
1 excluding lines 4 to 17):
1
1 sed '4,17!s/hello/world/' input.txt > output.txt
1