Hitachi

Job Management Partner 1 Version 10 Job Management Partner 1/Advanced Shell Description, User's Guide, Reference, and Operator's Guide


sed command (replaces character strings in text)

Organization of this page

Format

sed [-a] [-E] [-n] [-r] [-u] command [input-file-path-name...]
sed [-a] [-E] [-n] [-r] [-u] [-e command]... [-f script-file-path-name]... [input-file-path-name...]

Description

This command sends text from a file or the standard input to the standard output with specified character strings replaced.

Arguments

-a

Specifies that if a parsing error occurs in an editing command, no new file is to be created nor is any existing file to be overwritten (which could otherwise occur unintentionally). This option is used to maintain control over when the pattern space output file used by the w command or by the s command's w flag is created. The pattern space output file is created when the w command or the s command's w flag is applied. If the -a option is not specified, the pattern space output file is created during parsing of the w command or the s command's w flag.

-E|-r

Specifies that any patterns specified in the command are to be handled as extended regular expressions. The -E and -r options function identically.

-n

Specifies that output of the pattern space to the standard output is to be suppressed. When this option is specified, output of the pattern space to the standard output will not be performed unless the p or P command is executed.

-u

In Windows, specifies that output buffering of the execution results to the standard output is to be suppressed.

In UNIX, specifies that the execution results are to be buffered on a per-record basis when output to the standard output.

command|-e command

Specifies a command for editing the input file. More than one -e option can be specified. If you specify more than one -e option, the commands will be executed in the order they are specified. When the -f option is not used, command can be specified without the preceding -e option.

-f script-file-path-name

Specifies the path name of a script file. The script file contains edit commands for editing the records in the input file. More than one -f option can be specified. If you specify more than one -f option, or if you specify a combination of multiple -f and -e options, the execution order of the commands will be the order in which they were specified.

input-file-path-name

Specifies the path name of an input file to be edited. More than one input file path name can be specified. If no path name is specified, the standard input is read. If more than one file is specified, each file is opened when processing of the previous file reaches the end of the file, and reading of input records from the newly opened file begins.

Editing command description format

The following is the description format for an editing command for editing an input file:

[address[,address]] command[ arguments]

An address for identifying a record to be retrieved for editing can be specified in address. The address can be a record line number or a search pattern character string to be used for matching

The editing command to be applied to the retrieved records is specified in command.

Arguments to be passed to the editing command can be specified in arguments.

As each record is read from the input file, it is compared to the line number or search pattern character string specified in address. If there is a match, the editing specified in the command is performed. If address is omitted, all records will be retrieved as targets for editing. The results of executing the editing command are output to the standard output.

Addresses

Addresses are used to identify the records from the input file that are to be edited.

Pattern space and hold space

The sed command maintains two workspaces for text editing, called the pattern space and the hold space.

The pattern space stores records that are read from the input file.

The flow of processing in the pattern space is as follows:

  1. Reads one record, delimited by the end-of-line code, from the input file.

    In Windows, the end-of-line code is [CR] + [LF] or [LF]. In UNIX, the end-of-line code is [LF]. In UNIX, if the input file uses [CR] + [LF] for the end-of-line code, the [CR] is stored in the pattern space.

  2. Copies the contents of the input record into the pattern space.

  3. Executes the editing command if the pattern space includes a line number specified in the address or the search pattern character string matches a character string in the pattern space.

    If the command to be executed is the D command and part of the input record remains in the pattern space after the D command has executed, steps 1 and 2 are skipped.

  4. Outputs the contents of the pattern space to the standard output, unless the -n option has been specified.

  5. Clears the contents of the pattern space.

The hold space is used as a temporary work area for operations such as saving the contents of the pattern space in the hold space and then returning the contents of the hold space to the pattern space.

Editing commands

The following editing commands can be used in sed:

[address[,address]]{ command-list}

Creates a group of multiple editing commands that are to be applied to the matched input records. The editing commands are delimited by an end-of-line character or a semicolon (;). If a right curly bracket (}) is specified on the same line as the last editing command, there must be a semicolon after the command name.

[address]a\(end-of-line)

text

Outputs the text set in text to the standard output before the next input record is read. To output multiple records, you must specify \ immediately before each end-of-line character.

In the following example, two records are sent to the standard output before the next input record is read.

a\(end-of-line)
text1\(end-of-line)
text2
[address[,address]]b[label]

Branches to the :label command defined by the specified label label. If label is not specified, branching is to the end of the script.

[address[,address]]c\(end-of-line)

text

Clears the contents of the pattern space. When no address or a single address is specified, the text expressed in text is sent to the standard output. When two addresses are specified, the text expressed in text is sent to the standard output after processing the last record in the selected range. If multiple records are output, you must specify \ immediately before each end-of-line character.

After the pattern space is cleared, the next input record is read and execution restarts from the beginning, without executing any commands that appear after the c command.

[address[,address]]d

Clears the contents of the pattern space. The contents of the pattern space are sent to the standard output before the pattern space is cleared. Then the next input record is read and execution restarts from the beginning, without executing any commands that appear after the d command.

[address[,address]]D

When the pattern space holds multiple records, deletes everything up to the first end-of-line character. The contents of the pattern space are not sent to the standard output. The next input record is then read and execution restarts from the beginning, without executing any commands that appear after the D command.

If the pattern space is empty as a result of executing the D command, the next input record is read and execution restarts from the beginning of the command.

[address[,address]]g

Copies the contents of the hold space into the pattern space. The previous contents of the pattern space are discarded.

[address[,address]]G

Appends the contents of the hold space to the pattern space, separated from the records already stored in the pattern space by the end-of-line character.

[address[,address]]h

Copies the contents of the pattern space into the hold space. The previous contents of the hold space are discarded.

[address[,address]]H

Appends the contents of the pattern space to the hold space, separated from the records already stored in the hold space by the end-of-line character.

[address]i\(end-of-line)

text

Sends the text expressed in text to the standard output before storing the current input record in the pattern space. To output multiple records, you must specify a backslash (\) immediately before each end-of-line character.

[address[,address]]l

Sends the contents of the pattern space to the standard output. For data other than single-byte characters (in the range 0x20 to 0x7e), the space, and multibyte characters, each byte is output as a three-digit octal number preceded by a backslash (\). The backslash itself is output as \\, and the control codes shown in the following table are output as escape characters.

Control code

Escape character that is output

Alert character (bell)

\a

Backspace character

\b

Formfeed character (page break)

\f

End-of-line character. Note that the end-of-line character at the end of a lone line (or of the last line in the case of multiple lines) is not output.

\n

Carriage return character

\r

Tab character

\t

Vertical tab character

\v

The value for the output width of a record is determined in the following priority order:

  1. Value of the COLUMNS environment variable

  2. For output to the console, width of the console screen

  3. 60 single-byte characters

The dollar sign ($) is output at the end of each record. If a record exceeds the output width, it is split and a backslash (\) is set at the split location.

Note that the COLUMNS environment variable cannot be defined in a job definition script in JP1/Advanced Shell.

[address[,address]]n

Sends the current contents of the pattern space to the standard output and then reads the next input record from the input file and stores it in the pattern space. The current line number is incremented by 1. If the -n option was specified, the current contents of the pattern space are not sent to the standard output.

[address[,address]]N

Reads the next input record from the input file and appends it to the pattern space, separated from the records already stored in the pattern space by the end-of-line character. The current line number is incremented by 1.

[address[,address]]p

Sends the contents of the pattern space to the standard output.

[address[,address]]P

If the pattern space contains multiple records, outputs the contents only up to the first end-of-line character. If the pattern space contains only one record, this command is the same as the p command.

[address]q

Terminates processing of the script. No further execution of commands or reading of input records is performed after this command. Unless the -n option was specified, the contents of the pattern space are sent to the standard output at the time of termination. In addition, any records that had been added using the a or r command are output.

[address]r path-name

Before reading the next input record, reads the file specified in path-name and outputs its contents to the standard output. Any errors that occur during input of the file specified in path-name are ignored.

In Windows, end-of-line codes in the file are output as [CR] + [LF].

In UNIX, end-of-line codes in the file are output as is.

[address[,address]]s/pattern/replacement/flags

Substitutes the replacement character string (replacement) for the first character string that matches the pattern (pattern) in the pattern space. Instead of using a forward slash (/) to separate s, pattern, and replacement, you can use any single-byte character except for a backslash (\) or the end-of-line character. To include the separator character within pattern or replacement, it must be preceded by a backslash (\).

You can specify a regular expression for pattern.

The following characters can be used in the replacement character string for the indicated purposes:

  • &: The ampersand (&) is replaced by the character string that matches pattern. To handle an & as a character to be replaced, it must be preceded by a backslash (\).

  • \N (where N is a digit from 1 through 9): The \N is replaced by the character string that matches a tagged regular expression enclosed in parentheses (( )) in pattern. The numeric digit (N) specifies the sequential order of the tagged regular expression to be matched.

  • \: To include an end-of-line code, specify backslash (\) immediately before the end-of-line code.

The flags that can be specified in flags include the values shown below. The flags are optional. More than one can be specified.

N

Only replace the N-th matched pattern in the pattern space.

g

Globally replace all the character strings that match the pattern in the pattern space, not just the first one.

p

If any substitution was made, output the contents of the pattern space to the standard output.

w path-name

If any substitution was made, output the contents of the pattern space to the file specified in path-name. If an existing file is specified in path-name, the following occurs:

  • If the -a option was not specified

    Regardless of whether a substitution was made, the prior contents before execution of the sed command are discarded.

  • If the -a option was specified

    If a substitution was made, the prior contents before execution of the sed command are discarded.

In Windows, the end-of-line code for the file is output as [CR] + [LF].

[address[,address]]t[label]

Branches to the : (colon) command defined by the specified label (label) if a substitution has been performed by the s command since the last input record was read or since the previously-executed t command was executed. If no label is specified, branching is to the end of the script.

[address[,address]] w path-name

Writes the contents of the pattern space to the file specified in path-name. If an existing file is specified in path-name, the following occurs:

  • If the -a option was not specified

    Regardless of whether there is a match with the address (address), the prior contents before execution of the sed command are discarded.

  • If the -a option was specified

    If there is a match with the address (address), the prior contents before execution of the sed command are discarded.

In Windows, the end-of-line code for the file is output as [CR] + [LF].

[address[,address]]x

Exchanges the contents of the pattern space and the contents of the hold space.

[address[,address]]y/string1/string2/

Searches the contents of the pattern space and replaces each character string specified in string1 with the character string specified in string2 (this is a character-by-character replacement, so each character position in string1 is replaced with the character at the corresponding position in string2).

The number of characters in string1 and string2 must be the same.

To specify an end-of-line character in string1 or string2, specify \n. Instead of using a / to separate y, string1, and string2, you can use any other single-byte character except for \ or the end-of-line character.

[address[,address]]!command or [address[,address]]!{command-list}

Applies the command, or list of grouped commands, to the records that are not selected by the address (address).

:label

Defines a label for the branch destinations specified in the b and t commands. The : (colon) command itself performs no processing.

[address]=

Outputs the current line number to the standard output as a single record.

(blank line)

Blank lines are ignored.

#

Indicates a comment. The hash mark (#) and everything following it is treated as a comment. Note that if the first column of the first record of the script file begins with #n, the -n option is assumed.

Escape characters

The escape characters listed below can be used within an address search pattern, the text portion of the a, c, and i commands, pattern and replacement character strings of the s command, and the search characters and replacement characters of the y command.

Escape character

Meaning

\a

Alert character (bell)

\b

Backspace character#1

\f

Formfeed character (page break)

\n

End-of-line character#2

\r

Carriage return character

\t

Tab character

\v

Vertical tab character

\xhex

Character represented by a one- or two-digit hexadecimal value (0 to 9, a to f, A to F)#3

\c

Any literal character (for example, \" for ")

\\

A single backslash character

#1

If you specify this in an address search pattern or in the pattern in the s command, it is treated as the \b regular expression operator. However, if you specify it in a character class enclosed in square brackets ([ ]), it is treated as the backspace character.

#2

In Windows, this is output as [CR] + [LF] when specified within the text portion of the a, c, and i commands.

#3

There are values that cannot be specified in a pattern depending on the character encoding at the time of execution. The values that can be specified for each character encoding are listed below in hexadecimal. Execution terminates with an error if you specify any other value.

[Figure] Shift JIS

0x01-0x80, 0xA0-0xDF, 0xFD-0xFF

[Figure] UTF-8

0x01-0xBF, 0xFE-0xFF

[Figure] EUC

0x01-0x8D, 0x90-0xA0, 0xFF

[Figure] C

0x01-0xFF

Return codes

Return code

Meaning

0

Normal termination

1 or greater

Error termination

Usage examples