sed: sed script overview
1
1 3.1 'sed' script overview
1 =========================
1
1 A 'sed' program consists of one or more 'sed' commands, passed in by one
1 or more of the '-e', '-f', '--expression', and '--file' options, or the
1 first non-option argument if zero of these options are used. This
1 document will refer to "the" 'sed' script; this is understood to mean
1 the in-order concatenation of all of the SCRIPTs and SCRIPT-FILEs passed
1 in. ⇒Overview.
1
1 'sed' commands follow this syntax:
1
1 [addr]X[options]
1
1 X is a single-letter 'sed' command. '[addr]' is an optional line
1 address. If '[addr]' is specified, the command X will be executed only
1 on the matched lines. '[addr]' can be a single line number, a regular
1 expression, or a range of lines (⇒sed addresses). Additional
1 '[options]' are used for some 'sed' commands.
1
1 The following example deletes lines 30 to 35 in the input. '30,35'
1 is an address range. 'd' is the delete command:
1
1 sed '30,35d' input.txt > output.txt
1
1 The following example prints all input until a line starting with the
1 word 'foo' is found. If such line is found, 'sed' will terminate with
1 exit status 42. If such line was not found (and no other error
1 occurred), 'sed' will exit with status 0. '/^foo/' is a
1 regular-expression address. 'q' is the quit command. '42' is the
1 command option.
1
1 sed '/^foo/q42' input.txt > output.txt
1
1 Commands within a SCRIPT or SCRIPT-FILE can be separated by
1 semicolons (';') or newlines (ASCII 10). Multiple scripts can be
1 specified with '-e' or '-f' options.
1
1 The following examples are all equivalent. They perform two 'sed'
1 operations: deleting any lines matching the regular expression '/^foo/',
1 and replacing all occurrences of the string 'hello' with 'world':
1
1 sed '/^foo/d ; s/hello/world/' input.txt > output.txt
1
1 sed -e '/^foo/d' -e 's/hello/world/' input.txt > output.txt
1
1 echo '/^foo/d' > script.sed
1 echo 's/hello/world/' >> script.sed
1 sed -f script.sed input.txt > output.txt
1
1 echo 's/hello/world/' > script2.sed
1 sed -e '/^foo/d' -f script2.sed input.txt > output.txt
1
1 Commands 'a', 'c', 'i', due to their syntax, cannot be followed by
1 semicolons working as command separators and thus should be terminated
1 with newlines or be placed at the end of a SCRIPT or SCRIPT-FILE.
1 Commands can also be preceded with optional non-significant whitespace
1 characters. ⇒Multiple commands syntax.
1