sed: Overview
1
1 2.1 Overview
1 ============
1
1 Normally 'sed' is invoked like this:
1
1 sed SCRIPT INPUTFILE...
1
1 For example, to replace all occurrences of 'hello' to 'world' in the
1 file 'input.txt':
1
1 sed 's/hello/world/' input.txt > output.txt
1
1 If you do not specify INPUTFILE, or if INPUTFILE is '-', 'sed'
1 filters the contents of the standard input. The following commands are
1 equivalent:
1
1 sed 's/hello/world/' input.txt > output.txt
1 sed 's/hello/world/' < input.txt > output.txt
1 cat input.txt | sed 's/hello/world/' - > output.txt
1
1 'sed' writes output to standard output. Use '-i' to edit files
1 in-place instead of printing to standard output. See also the 'W' and
1 's///w' commands for writing output to other files. The following
1 command modifies 'file.txt' and does not produce any output:
1
1 sed -i 's/hello/world/' file.txt
1
1 By default 'sed' prints all processed input (except input that has
1 been modified/deleted by commands such as 'd'). Use '-n' to suppress
1 output, and the 'p' command to print specific lines. The following
1 command prints only line 45 of the input file:
1
1 sed -n '45p' file.txt
1
1 'sed' treats multiple input files as one long stream. The following
1 example prints the first line of the first file ('one.txt') and the last
1 line of the last file ('three.txt'). Use '-s' to reverse this behavior.
1
1 sed -n '1p ; $p' one.txt two.txt three.txt
1
1 Without '-e' or '-f' options, 'sed' uses the first non-option
1 parameter as the SCRIPT, and the following non-option parameters as
1 input files. If '-e' or '-f' options are used to specify a SCRIPT, all
1 non-option parameters are taken as input files. Options '-e' and '-f'
1 can be combined, and can appear multiple times (in which case the final
1 effective SCRIPT will be concatenation of all the individual SCRIPTs).
1
1 The following examples are equivalent:
1
1 sed 's/hello/world/' input.txt > output.txt
1
1 sed -e 's/hello/world/' input.txt > output.txt
1 sed --expression='s/hello/world/' input.txt > output.txt
1
1 echo 's/hello/world/' > myscript.sed
1 sed -f myscript.sed input.txt > output.txt
1 sed --file=myscript.sed input.txt > output.txt
1