sed — Start the sed noninteractive stream editor

Format

Description

The sed command applies a set of editing subcommands contained in script to each argument input file.

If more than one file is specified, they are concatenated and treated as a single large file. script is the arguments of all –e and –f options and the contents of all scriptfiles. You can specify multiple –e and –f options; commands are added to script in the order specified.

If you did not specify file, sed reads the standard input.

sed reads each input line into a special area known as the pattern buffer. Certain subcommands [gGhHx] use a second area called the hold buffer. By default, after each pass through the script, sed writes the final contents of the pattern buffer to the standard output.

Options

Start of change–BEnd of change
Start of changeDisables the automatic conversion of tagged files. This option is ignored if the filecodeset or pgmcodeset options (-W option) are specified.End of change
–E
Uses extended regular expressions. Normally, sed uses basic regular expressions.
–e script
Adds the editing subcommands script to the end of the script.
–f scriptfile
Adds the subcommands in the file scriptfile to the end of the script.
–n
Suppresses all output except that generated by explicit subcommands in the sed script [acilnpPr]
Start of change-W option[,option]...End of change
Specifies z/OS-specific options. The option keywords are case-sensitive. Possible options are:
filecodeset=codeset
Performs text conversion from one code set to another when reading from the file. The coded character set of the file is codeset. codeset can be a code set name known to the system or a numeric coded character set identifier (CCSID). Note that the command iconv -l lists existing CCSIDs along with their corresponding code set names. The filecodeset and pgmcodeset options can be used on files with any file tag.

If pgmcodeset is specified but filecodeset is omitted, then the default file code set is ISO8859-1 even if the file is tagged with a different code set. If neither filecodeset nor pgmcodeset is specified, text conversion will not occur unless automatic conversion is enabled or the _TEXT_CONV environment variable indicates text conversion. For more information about text conversion, see Controlling text conversion for z/OS UNIX shell commands.

If filecodeset or pgmcodeset is specified, then automatic conversion is disabled for this command invocation and the -B option is ignored if it is also specified. See z/OS UNIX System Services Planning for more information about automatic conversion.

Start of changeWhen specifying values for filecodeset, use the values that Unicode Service supports. For more information about supported code sets, see z/OS Unicode Services User's Guide and Reference.End of change

Start of changepgmcodeset=codesetEnd of change
Start of changePerforms text conversion from one code set to another when reading from the file. The coded character set of the program (command) is codeset. codeset can be a code set name known to the system or a numeric coded character set identifier (CCSID). Note that the command iconv -l lists existing CCSIDs along with their corresponding code set names. The filecodeset and pgmcodeset options can be used on files with any file tag.

If filecodeset is specified but pgmcodeset is omitted, then the default program code set is IBM-1047. If neither filecodeset nor pgmcodeset is specified, text conversion will not occur unless automatic conversion is enabled or the _TEXT_CONV environment variable indicates text conversion. For more information about text conversion, see Controlling text conversion for z/OS UNIX shell commands.

If filecodeset or pgmcodeset is specified, then automatic conversion is disabled for this command invocation and the -B option is ignored if it is also specified. See z/OS UNIX System Services Planning for more information about automatic conversion.

Restriction: The only supported values for pgmcodeset are IBM-1047 and 1047.
End of change

If you need only one script argument, you can omit the –e and use the first form of the command.

sed subcommands are similar to those of the interactive text editor ed, except that sed subcommands necessarily view the input text as a stream rather than as a directly addressable file.

Each line of a sed script consists of one or more editing commands. The commands can be preceded by either semicolons or blanks, or both. Each editing command contains up to two addresses, a single letter command, and possible command arguments. The last editing command is followed with a terminating newline. The newline is optional in script strings typed on the command line.
[addr[,addr]] command [arguments]

Subcommands

sed subcommands necessarily view the input text as a stream rather than as a directly addressable file. Script subcommands can begin with zero, one, or two addresses, as in ed.
  • Zero-address subcommands refer to every input line.
  • One-address subcommands select only those lines matching that address.
  • Two-address subcommands select those input line ranges starting with a match on the first address up to an input line matching the second address, inclusive. If the second address is a number less than or equal to the line number first selected, only one line is selected.
Permissible addressing constructions are:
n
The number n matches only the nth input line.
$
This address matches the last input line.
/regexp/
This address selects an input line matching the specified regular expression regexp. If you do not want to use slash (/) characters around the regular expression, use a different character (but not backslash or newline) and put a backslash (\) before the first one. For example, if you want to use % to enclose the regular expression, write \%regexp%.

If an regexp is empty (that is, no pattern is specified) sed behaves as if the last regexp used in the last command applied (either as an address or as part of a substitute command) was specified.

A command can be preceded by a '!' character, in which case the command is applied if the addresses do not select the pattern space. When the variable _UNIX03=YES is set, one or more '!' characters are allowed, and it is not allowed to follow a '!' character with <blanks>s. When the variable _UNIX03 is unset or is not set to YES, only one '!' character is allowed, and it is not allowed to follow a '!' character with <blanks>s.

Start of changeThe following sed subcommand summary shows the subcommands with the maximum number of legitimate addresses. A subcommand can be given fewer than the number of addresses specified, but not more. A subcommand with the form [a] command supports up to one address and a subcommand with the form [a[,b]] command supports up to two addresses. All other subcommands do not support any addresses.
Start of change[a]a\End of change
Start of changeAppends subsequent text lines from the script to the standard output. sed writes the text after completing all other script operations for that line and before reading the next record. Text lines are ended by the first line that does not end with a backslash (\). sed does not treat the \ characters on the end of lines as part of the text.End of change
Start of change[a[,b]]b [label]End of change
Start of changeBranches to :label. If you omit label, sed branches to the end of the script.End of change
Start of change[a[,b]]c\End of change
Start of changeChanges the addressed lines by deleting the contents of the pattern buffer (input line) and sending subsequent text (similar to the a command) to the standard output. When you specify two addresses, sed delays text output until the final line in the range of addresses; otherwise, the behavior would surprise many users. The rest of the script is skipped for each addressed line except the last.End of change
Start of change[a[,b]]dEnd of change
Start of changeDeletes the contents of the pattern buffer (input line) and restarts the script with the next input line.End of change
Start of change[a[,b]]DEnd of change
Start of changeDeletes the pattern buffer only up to and including the first newline. Then it restarts the script from the beginning and applies it to the text left in the pattern buffer.End of change
Start of change[a[,b]]gEnd of change
Start of changeGrabs a copy of the text in the hold buffer and places it in the pattern buffer, overwriting the original contents.End of change
Start of change[a[,b]]GEnd of change
Start of changeGrabs a copy of the text in the hold buffer and appends it to the end of the pattern buffer after appending a newline.End of change
Start of change[a[,b]]hEnd of change
Start of changeHolds a copy of the text in the pattern buffer by placing it in the hold buffer, overwriting its original contents.End of change
Start of change[a[,b]]HEnd of change
Start of changeHolds a copy of the text in the pattern buffer by appending it to the end of the hold buffer after appending a newline.End of change
Start of change[a]i\End of change
Start of changeInserts text. This subcommand is similar to the a subcommand, except that its text is output immediately.End of change
Start of change[a[,b]]lEnd of change
Start of changeLists the pattern buffer (input line) to the standard output so that nonprintable characters are visible. The end-of-line is represented by $, and the characters \\, \a, \b, \f, \r, \t, and \v are printed as escape sequences. Each byte of a nonprintable double-byte character appears as an escape sequence or as a 3-digit octal number. This subcommand is analogous to the l subcommand in ed.

sed folds long lines to suit the output device, indicating the point of folding with a backslash (\).

End of change
Start of change[a[,b]]nEnd of change
Start of changePrints the pattern space on standard output if the default printing of the pattern space is not suppressed (because of the -n option). The next line of input is then read, and the processing of the line continues from the location of the n command in the script.End of change
Start of change[a[,b]]NEnd of change
Start of changeAppends the next line of input to the end of the pattern buffer, using a new line to separate the appended material from the original. The current line number changes.End of change
Start of change[a[,b]]pEnd of change
Start of changePrints the text in the pattern buffer to the standard output. The –n option does not disable this form of output. If you do not use –n, the pattern buffer is printed twice.End of change
Start of change[a[,b]]PEnd of change
Start of changeOperates like the p subcommand, except that it prints the text in the pattern buffer only up to and including the first newline character.End of change
Start of change[a]qEnd of change
Start of changeQuits sed, skipping the rest of the script and reading no more input lines.End of change
Start of change[a]r fileEnd of change
Start of changeStart of changeReads text from file and writes it to the standard output before reading the next input line. The text conversion specified for the sed command (for example, the -B and -W option) is used. The timing of this operation is the same as for the a subcommand. If file does not exist or cannot be read, sed treats it as an empty file.End of changeEnd of change
Start of change[a[,b]]s/reg/ sub/[gpn] [wfile]End of change
Start of changeSubstitutes the new text string sub for text matching the regular expression, reg. Normally, the s subcommand replaces only the first such matching string in each input line. You can use any single printable character other than space or newline instead of the slash (/) to delimit reg and sub. The delimiter itself may appear as a literal character in reg or sub if you precede it with a backslash (\). You can omit the trailing delimiter.
If an ampersand (&) appears in sub, sed replaces it with the string matching reg. A \n in reg matches an embedded newline in the pattern buffer (resulting, for example, from an N subcommand). The subcommand can be followed by a combination of the following:
n
Substitutes only the nth occurrence of regexp.
g
Replaces all non-overlapping occurrences of regexp rather than the default first occurrence. If both g and n are specified, the last one specified takes precedence.
p
Executes the print (p) subcommand only if a successful substitution occurs.
w file
Writes the contents of the pattern buffer to the end of file, if a substitution occurs. Start of changeThe text conversion specified for the sed command (for example, the -B and -W option) is used.End of change When the variable _UNIX03=YES is set, the file must be preceded with one or more <blank>s. When the variable _UNIX03 is unset or is not set to YES, zero <blank> separation between w and file is allowed.
End of change
Start of change[a[,b]]t [label]End of change
Start of changeBranches to the indicated label if a successful substitution occurred since either reading the last input line or running the last t subcommand. If you do not specify label, sed branches to the end of the script.End of change
Start of change[a[,b]]w fileEnd of change
Start of changeWrites the text in the pattern buffer to the end of file. Start of changeThe text conversion specified for the sed command (for example, the -B and -W option) is used.End of changeEnd of change
Start of change[a[,b]]xEnd of change
Start of changeExchanges the text in the hold buffer with that in the pattern buffer.End of change
Start of change[a[,b]]y/set1/set2/End of change
Start of changeTransliterates any input character occurring in set1 to the corresponding element of set2. The sets must be the same length. You can use any character other than backslash or newline instead of the slash to delimit the strings.

If the variable _UNIX03=YES is set and a backslash followed by an 'n' appear in set1 or set2, the two characters are handled as a single newline character. If the variable _UNIX03 is unset or is not set to YES, the two characters are handled as a single character 'n'.

If the delimiter is not n, within set1 and set2, the delimiter itself can be used as a literal character if it is preceded by a backslash. If a backslash character is immediately followed by a backslash character in set1 or set2, the two backslash characters are counted as a single literal backslash character.

End of change
Start of change[a[,b]]{End of change
Start of changeGroups all commands until the next matching } subcommand, so that sed runs the entire group only if the { subcommand is selected by its addresses.End of change
:label
Designates a label, which can be the destination of a bor t subcommand.
#
Treats the script line as a comment unless it is the first line in the script. Including the first line in a script as #n is equivalent to specifying –n on the command line. An empty script line is also treated as a comment.
Start of change[a]=End of change
Start of changeWrites the decimal value of the current line number to the standard output.End of change
End of change

Examples

  1. This filter switches desserts in a menu:
    sed 's/cake\(ic\)*/cookies/g'
  2. Start of changeTo substitute a pattern in a text file that contains ASCII characters, using the sed stream-oriented text editor and assuming that
    • The text file is untagged and you do not want to tag it or enable automatic conversion, and
    • You cannot alter the tag (for example, you are processing an untagged public text file or a read-only text file)
    then issue:
    sed -W filecodeset=819,pgmcodeset=1047 ’s/pattern1/pattern2/w myOutFile’ myAsciiFile
    End of change
  3. To substitute a pattern in a text file using the sed stream-oriented text editor, assuming that automatic conversion was enabled but the text file is incorrectly tagged as Start of changeUTF-8End of change:
    sed -B 's/pattern1/pattern2/w myOutputFile' myMisTaggedFile

Environment variables

sed uses the following environment variables:
COLUMNS
Contains the width of the screen in columns. If set, sed uses this value to fold long lines on output. Otherwise, sed uses a default screen width of 80.
Start of change_TEXT_CONVEnd of change
Start of changeContains text conversion information for the command. The text conversion information is not used when either the -B option or the filecodeset or pgmcodeset option (-W option) is specified. For more information about text conversion, see Controlling text conversion for z/OS UNIX shell commands.End of change
_UNIX03
For more information about the effect of _UNIX03 on this command, see Shell commands changed for UNIX03.

Localization

sed uses the following localization environment variables:
  • LANG
  • LC_ALL
  • LC_COLLATE
  • LC_CTYPE
  • LC_MESSAGES
  • LC_SYNTAX
  • NLSPATH

See Localization for more information.

Exit values

0
Successful completion
1
Failure due to any of the following:
  • Missing script
  • Too many script arguments
  • Too few arguments
  • Unknown option
  • Inability to open script file
  • No noncomment subcommand
  • Label not found in script
  • Unknown subcommand
  • Nesting ! subcommand not permitted
  • No \ at end of subcommand
  • End-of-file in subcommand
  • No label in subcommand
  • Badly formed file name
  • Inability to open file
  • Insufficient memory to compile subcommand
  • Bad regular expression delimiter
  • No remembered regular expression
  • Regular expression error
  • Insufficient memory for buffers
  • y subcommand not followed by a printable character as separator
  • The strings are not the same length
  • Nonmatching { and } subcommands
  • Garbage after command
  • Too many addresses for command
  • Newline or end-of-file found in pattern
  • Input line too long
  • Pattern space overflow during G subcommand
  • Hold space overflow during H subcommand
  • Inability to chain subcommand
  • Start of changeThe code set is not validEnd of change
  • Start of changeCould not turn off automatic conversionEnd of change
  • Start of changeCould not perform requested text conversionEnd of change

Messages

Possible error messages include:
badly formed filename for command command
The given subcommand required a file name, but its operand did not have the syntax of a file name.
subcommand command needs a label
The specified subcommand required a label, but you did not supply one.
must have at least one (noncomment) command
The input to sed must contain at least one active subcommand (that is, a subcommand that is not a comment).
No remembered regular expression
You issued a subcommand that tried to use a remembered regular expression; for example, s//abc. However, there is no remembered regular expression yet. Change the subcommand to use an explicit regular expression.

Limits

sed allows a limit of 28000 lines per file. It does not allow the NUL character.

Portability

POSIX.2, X/Open Portability Guide, UNIX systems.

Start of changeThe –B, –E, and -W options are extensions of the POSIX standard.End of change

Related information

awk, diff, ed, grep, vi

For more information about regexp, see Regular expressions (regexp).