sed: Multiple commands syntax
1
1 3.8 Multiple commands syntax
1 ============================
1
1 There are several methods to specify multiple commands in a 'sed'
1 program.
1
1 Using newlines is most natural when running a sed script from a file
1 (using the '-f' option).
1
1 On the command line, all 'sed' commands may be separated by newlines.
1 Alternatively, you may specify each command as an argument to an '-e'
1 option:
1
1 $ seq 6 | sed '1d
1 3d
1 5d'
1 2
1 4
1 6
1
1 $ seq 6 | sed -e 1d -e 3d -e 5d
1 2
1 4
1 6
1
1 A semicolon (';') may be used to separate most simple commands:
1
1 $ seq 6 | sed '1d;3d;5d'
1 2
1 4
1 6
1
1 The '{','}','b','t','T',':' commands can be separated with a
1 semicolon (this is a non-portable GNU 'sed' extension).
1
1 $ seq 4 | sed '{1d;3d}'
1 2
1 4
1
1 $ seq 6 | sed '{1d;3d};5d'
1 2
1 4
1 6
1
1 Labels used in 'b','t','T',':' commands are read until a semicolon.
1 Leading and trailing whitespace is ignored. In the examples below the
1 label is 'x'. The first example works with GNU 'sed'. The second is a
1 portable equivalent. For more information about branching and labels
1 ⇒Branching and flow control.
1
1 $ seq 3 | sed '/1/b x ; s/^/=/ ; :x ; 3d'
1 1
1 =2
1
1 $ seq 3 | sed -e '/1/bx' -e 's/^/=/' -e ':x' -e '3d'
1 1
1 =2
1
1 3.8.1 Commands Requiring a newline
1 ----------------------------------
1
1 The following commands cannot be separated by a semicolon and require a
1 newline:
1
1 'a','c','i' (append/change/insert)
1
1 All characters following 'a','c','i' commands are taken as the text
1 to append/change/insert. Using a semicolon leads to undesirable
1 results:
1
1 $ seq 2 | sed '1aHello ; 2d'
1 1
1 Hello ; 2d
1 2
1
1 Separate the commands using '-e' or a newline:
1
1 $ seq 2 | sed -e 1aHello -e 2d
1 1
1 Hello
1
1 $ seq 2 | sed '1aHello
1 2d'
1 1
1 Hello
1
1 Note that specifying the text to add ('Hello') immediately after
1 'a','c','i' is itself a GNU 'sed' extension. A portable,
1 POSIX-compliant alternative is:
1
1 $ seq 2 | sed '1a\
1 Hello
1 2d'
1 1
1 Hello
1
1 '#' (comment)
1
1 All characters following '#' until the next newline are ignored.
1
1 $ seq 3 | sed '# this is a comment ; 2d'
1 1
1 2
1 3
1
1
1 $ seq 3 | sed '# this is a comment
1 2d'
1 1
1 3
1
1 'r','R','w','W' (reading and writing files)
1
1 The 'r','R','w','W' commands parse the filename until end of the
1 line. If whitespace, comments or semicolons are found, they will
1 be included in the filename, leading to unexpected results:
1
1 $ seq 2 | sed '1w hello.txt ; 2d'
1 1
1 2
1
1 $ ls -log
1 total 4
1 -rw-rw-r-- 1 2 Jan 23 23:03 hello.txt ; 2d
1
1 $ cat 'hello.txt ; 2d'
1 1
1
1 Note that 'sed' silently ignores read/write errors in
1 'r','R','w','W' commands (such as missing files). In the following
1 example, 'sed' tries to read a file named ''hello.txt ; N''. The
1 file is missing, and the error is silently ignored:
1
1 $ echo x | sed '1rhello.txt ; N'
1 x
1
1 'e' (command execution)
1
1 Any characters following the 'e' command until the end of the line
1 will be sent to the shell. If whitespace, comments or semicolons
1 are found, they will be included in the shell command, leading to
1 unexpected results:
1
1 $ echo a | sed '1e touch foo#bar'
1 a
1
1 $ ls -1
1 foo#bar
1
1 $ echo a | sed '1e touch foo ; s/a/b/'
1 sh: 1: s/a/b/: not found
1 a
1
1 's///[we]' (substitute with 'e' or 'w' flags)
1
1 In a substitution command, the 'w' flag writes the substitution
1 result to a file, and the 'e' flag executes the subsitution result
1 as a shell command. As with the 'r/R/w/W/e' commands, these must
1 be terminated with a newline. If whitespace, comments or
1 semicolons are found, they will be included in the shell command or
1 filename, leading to unexpected results:
1
1 $ echo a | sed 's/a/b/w1.txt#foo'
1 b
1
1 $ ls -1
1 1.txt#foo
1