File: sed.info, Node: Addresses overview, Next: Numeric Addresses, Up: sed addresses 4.1 Addresses overview ====================== Addresses determine on which line(s) the ‘sed’ command will be executed. The following command replaces any first occurrence of ‘hello’ with ‘world’ only on line 144: sed '144s/hello/world/' input.txt > output.txt If no address is specified, the command is performed on all lines. The following command replaces ‘hello’ with ‘world’, targeting every line of the input file. However, note that it modifies only the first instance of ‘hello’ on each line. Use the ‘g’ modifier to affect every instance on each affected line. sed 's/hello/world/' input.txt > output.txt Addresses can contain regular expressions to match lines based on content instead of line numbers. The following command replaces ‘hello’ with ‘world’ only on lines containing the string ‘apple’: sed '/apple/s/hello/world/' input.txt > output.txt An address range is specified with two addresses separated by a comma (‘,’). Addresses can be numeric, regular expressions, or a mix of both. The following command replaces ‘hello’ with ‘world’ only on lines 4 to 17 (inclusive): sed '4,17s/hello/world/' input.txt > output.txt Appending the ‘!’ character to the end of an address specification (before the command letter) negates the sense of the match. That is, if the ‘!’ character follows an address or an address range, then only lines which do _not_ match the addresses will be selected. The following command replaces ‘hello’ with ‘world’ only on lines _not_ containing the string ‘apple’: sed '/apple/!s/hello/world/' input.txt > output.txt The following command replaces ‘hello’ with ‘world’ only on lines 1 to 3 and from line 18 to the last line of the input file (i.e. excluding lines 4 to 17): sed '4,17!s/hello/world/' input.txt > output.txt
