sed: Command-Line Options
1
1 2.2 Command-Line Options
1 ========================
1
1 The full format for invoking 'sed' is:
1
1 sed OPTIONS... [SCRIPT] [INPUTFILE...]
1
1 'sed' may be invoked with the following command-line options:
1
1 '--version'
1 Print out the version of 'sed' that is being run and a copyright
1 notice, then exit.
1
1 '--help'
1 Print a usage message briefly summarizing these command-line
1 options and the bug-reporting address, then exit.
1
1 '-n'
1 '--quiet'
1 '--silent'
1 By default, 'sed' prints out the pattern space at the end of each
1 cycle through the script (⇒How 'sed' works Execution Cycle.).
1 These options disable this automatic printing, and 'sed' only
1 produces output when explicitly told to via the 'p' command.
1
1 '-e SCRIPT'
1 '--expression=SCRIPT'
1 Add the commands in SCRIPT to the set of commands to be run while
1 processing the input.
1
1 '-f SCRIPT-FILE'
1 '--file=SCRIPT-FILE'
1 Add the commands contained in the file SCRIPT-FILE to the set of
1 commands to be run while processing the input.
1
1 '-i[SUFFIX]'
1 '--in-place[=SUFFIX]'
1 This option specifies that files are to be edited in-place. GNU
1 'sed' does this by creating a temporary file and sending output to
1 this file rather than to the standard output.(1).
1
1 This option implies '-s'.
1
1 When the end of the file is reached, the temporary file is renamed
1 to the output file's original name. The extension, if supplied, is
1 used to modify the name of the old file before renaming the
1 temporary file, thereby making a backup copy(2)).
1
1 This rule is followed: if the extension doesn't contain a '*', then
1 it is appended to the end of the current filename as a suffix; if
1 the extension does contain one or more '*' characters, then _each_
1 asterisk is replaced with the current filename. This allows you to
1 add a prefix to the backup file, instead of (or in addition to) a
1 suffix, or even to place backup copies of the original files into
1 another directory (provided the directory already exists).
1
1 If no extension is supplied, the original file is overwritten
1 without making a backup.
1
1 Because '-i' takes an optional argument, it should not be followed
1 by other short options:
1 'sed -Ei '...' FILE'
1 Same as '-E -i' with no backup suffix - 'FILE' will be edited
1 in-place without creating a backup.
1
1 'sed -iE '...' FILE'
1 This is equivalent to '--in-place=E', creating 'FILEE' as
1 backup of 'FILE'
1
1 Be cautious of using '-n' with '-i': the former disables automatic
1 printing of lines and the latter changes the file in-place without
1 a backup. Used carelessly (and without an explicit 'p' command),
1 the output file will be empty:
1 # WRONG USAGE: 'FILE' will be truncated.
1 sed -ni 's/foo/bar/' FILE
1
1 '-l N'
1 '--line-length=N'
1 Specify the default line-wrap length for the 'l' command. A length
1 of 0 (zero) means to never wrap long lines. If not specified, it
1 is taken to be 70.
1
1 '--posix'
1 GNU 'sed' includes several extensions to POSIX sed. In order to
1 simplify writing portable scripts, this option disables all the
1 extensions that this manual documents, including additional
1 commands. Most of the extensions accept 'sed' programs that are
1 outside the syntax mandated by POSIX, but some of them (such as the
1 behavior of the 'N' command described in ⇒Reporting Bugs)
1 actually violate the standard. If you want to disable only the
1 latter kind of extension, you can set the 'POSIXLY_CORRECT'
1 variable to a non-empty value.
1
1 '-b'
1 '--binary'
1 This option is available on every platform, but is only effective
1 where the operating system makes a distinction between text files
1 and binary files. When such a distinction is made--as is the case
1 for MS-DOS, Windows, Cygwin--text files are composed of lines
1 separated by a carriage return _and_ a line feed character, and
1 'sed' does not see the ending CR. When this option is specified,
1 'sed' will open input files in binary mode, thus not requesting
1 this special processing and considering lines to end at a line
1 feed.
1
1 '--follow-symlinks'
1 This option is available only on platforms that support symbolic
1 links and has an effect only if option '-i' is specified. In this
1 case, if the file that is specified on the command line is a
1 symbolic link, 'sed' will follow the link and edit the ultimate
1 destination of the link. The default behavior is to break the
1 symbolic link, so that the link destination will not be modified.
1
1 '-E'
1 '-r'
1 '--regexp-extended'
1 Use extended regular expressions rather than basic regular
1 expressions. Extended regexps are those that 'egrep' accepts; they
1 can be clearer because they usually have fewer backslashes.
1 Historically this was a GNU extension, but the '-E' extension has
1 since been added to the POSIX standard
1 (http://austingroupbugs.net/view.php?id=528), so use '-E' for
1 portability. GNU sed has accepted '-E' as an undocumented option
1 for years, and *BSD seds have accepted '-E' for years as well, but
11 scripts that use '-E' might not port to other older systems. ⇒
Extended regular expressions ERE syntax.
1
1 '-s'
1 '--separate'
1 By default, 'sed' will consider the files specified on the command
1 line as a single continuous long stream. This GNU 'sed' extension
1 allows the user to consider them as separate files: range addresses
1 (such as '/abc/,/def/') are not allowed to span several files, line
1 numbers are relative to the start of each file, '$' refers to the
1 last line of each file, and files invoked from the 'R' commands are
1 rewound at the start of each file.
1
1 '--sandbox'
1 In sandbox mode, 'e/w/r' commands are rejected - programs
1 containing them will be aborted without being run. Sandbox mode
1 ensures 'sed' operates only on the input files designated on the
1 command line, and cannot run external programs.
1
1 '-u'
1 '--unbuffered'
1 Buffer both input and output as minimally as practical. (This is
1 particularly useful if the input is coming from the likes of 'tail
1 -f', and you wish to see the transformed output as soon as
1 possible.)
1
1 '-z'
1 '--null-data'
1 '--zero-terminated'
1 Treat the input as a set of lines, each terminated by a zero byte
1 (the ASCII 'NUL' character) instead of a newline. This option can
1 be used with commands like 'sort -z' and 'find -print0' to process
1 arbitrary file names.
1
1 If no '-e', '-f', '--expression', or '--file' options are given on
1 the command-line, then the first non-option argument on the command line
1 is taken to be the SCRIPT to be executed.
1
1 If any command-line parameters remain after processing the above,
1 these parameters are interpreted as the names of input files to be
1 processed. A file name of '-' refers to the standard input stream. The
1 standard input will be processed if no file names are specified.
1
1 ---------- Footnotes ----------
1
1 (1) This applies to commands such as '=', 'a', 'c', 'i', 'l', 'p'.
1 You can still write to the standard output by using the 'w' or 'W'
1 commands together with the '/dev/stdout' special file
1
1 (2) Note that GNU 'sed' creates the backup file whether or not any
1 output is actually changed.
1