Extended Commands (sed, a stream editor)
Next: Multiple commands syntax, Previous: Programming Commands, Up: sed scripts [Contents][Index]
3.7 Commands Specific to GNU sed
These commands are specific to GNU sed, so you must use them with care and only when you are sure that hindering portability is not evil. They allow you to check for GNU sed extensions or to do tasks that are required quite often, yet are unsupported by standard seds.
e [command]This command allows one to pipe input from a shell command into pattern space. Without parameters, the
ecommand executes the command that is found in pattern space and replaces the pattern space with the output; a trailing newline is suppressed.If a parameter is specified, instead, the
ecommand interprets it as a command and sends its output to the output stream. The command can run across multiple lines, all but the last ending with a back-slash.In both cases, the results are undefined if the command to be executed contains a NUL character.
Note that, unlike the
rcommand, the output of the command will be printed immediately; thercommand instead delays the output to the end of the current cycle.FPrint out the file name of the current input file (with a trailing newline).
Q [exit-code]This command accepts only one address.
This command is the same as
q, but will not print the contents of pattern space. Likeq, it provides the ability to return an exit code to the caller.This command can be useful because the only alternative ways to accomplish this apparently trivial function are to use the
-noption (which can unnecessarily complicate your script) or resorting to the following snippet, which wastes time by reading the whole file without any visible effect::eat $d Quit silently on the last line N Read another line, silently g Overwrite pattern space each time to save memory b eat
R filenameQueue a line of
filenameto be read and inserted into the output stream at the end of the current cycle, or when the next input line is read. Note that iffilenamecannot be read, or if its end is reached, no line is appended, without any error indication.As with the
rcommand, the special value/dev/stdinis supported for the file name, which reads a line from the standard input.T labelBranch to
labelonly if there have been no successfulsubstitutions since the last input line was read or conditional branch was taken. Thelabelmay be omitted, in which case the next cycle is started.v versionThis command does nothing, but makes
sedfail if GNUsedextensions are not supported, simply because other versions ofseddo not implement it. In addition, you can specify the version ofsedthat your script requires, such as4.0.5. The default is4.0because that is the first version that implemented this command.This command enables all GNU extensions even if
POSIXLY_CORRECTis set in the environment.W filenameWrite to the given filename the portion of the pattern space up to the first newline. Everything said under the
wcommand about file handling holds here too.zThis command empties the content of pattern space. It is usually the same as ‘
s/.*//’, but is more efficient and works in the presence of invalid multibyte sequences in the input stream. POSIX mandates that such sequences are not matched by ‘.’, so that there is no portable way to clearsed’s buffers in the middle of the script in most multibyte locales (including UTF-8 locales).
Next: Multiple commands syntax, Previous: Programming Commands, Up: sed scripts [Contents][Index]