Hitachi

JP1 Version 11 JP1/Advanced Shell Description, User's Guide, Reference, and Operator's Guide 


8.4.36 tr command (outputs character strings input from the standard input to the standard output while replacing or deleting characters on a byte-by-byte basis)

Syntax

Syntax 1
tr [-cst] [--check-multi-byte] character-string-1 character-string-2
Syntax 2
tr -d [-c] [--check-multi-byte] character-string-1
Syntax 3
tr -s [-c] [--check-multi-byte] character-string-1
Syntax 4
tr [-ds] [--check-multi-byte] character-string-1 character-string-2

Description

This command outputs character strings input from the standard input to the standard output while replacing or deleting characters on a byte-by-byte basis.

The command specified in syntax 1 replaces the character strings input from the standard input, and then outputs the results to the standard output. During replacement, each byte in character-string-1 contained in character strings in the standard input is replaced with a byte in character-string-2. If character-string-1 is longer than character-string-2, the last character of character-string-2 is assumed to be repeated until its length reaches character-string-1.

The command specified in syntax 2 deletes the characters (bytes) in character-string-1 contained in character strings input from the standard input, and then outputs the results to the standard output.

The command specified in syntax 3 replaces the character strings input from the standard input, and then outputs the results to the standard output. During replacement, if the character strings in the standard input contain characters (bytes) in character-string-1 in succession, consecutive identical characters are compressed into a single character.

The command specified in syntax 4 deletes and replaces the character strings input from the standard input, and then outputs the results to the standard output. During deletion, characters (bytes) in character-string-1 contained in character strings in the standard input are deleted. If the character strings after deletion of characters in character-string-1 contain characters (bytes) in character-string-2 in succession, consecutive identical characters are compressed into a single character.

Arguments

-c

--complement

Replaces character-string-1 with the complement of character-string-1 (that is, all characters not contained in character-string-1 ).

-d

--delete

Deletes the input characters existing in character-string-1 from the character strings in the standard input.

-s

--squeeze-repeats

In the character strings for which all deletion and replacement processes are complete, this option compresses consecutive identical characters (bytes) into a single byte. For syntax 1 and syntax 4, the character string to be replaced consists of characters (bytes) specified in character-string-2. For syntax 3, the character string to be replaced consists of characters (bytes) specified in character-string-1.

-t

--truncate-set1

If character-string-1 is longer than character-string-2, character-string-1 is truncated to the length of character-string-2 by deleting characters. Then, the character string is replaced.

--check-multi-byte

If a multibyte character is contained in character-string-1 or character-string-2, an error results.

character-string-1, character-string-2

Specify the character string to be replaced or deleted for the character strings input from the standard input. The character string is processed byte by byte.

You can specify a set of characters for character-string-1 and character-string-2 by using the following expression.

Specification format

Meaning

\ooo

Define ASCII code characters displayed in octal notation with 1 to 3 digits.

character-1-character-2

Range specification. Specify a character (one byte) in ASCII collating sequence.

Escape character

\a

Alert character (bell)

\b

Backspace character

\f

Formfeed character (page break)

\n

Linefeed character

\r

Carriage return character

\t

Tab character

\v

Vertical tab character

[:character-class:]

For character-string-1 and syntax 4 character-string-2, you can specify the following character classes. For syntax 1 character-string-2, you can specify lower or upper if all the following conditions are satisfied:

- The -c option is not specified.

- In character-string-1, specify lower or upper in the same position as character-string-2

alnum

Alphanumeric character

alpha

Alphabetic character

blank

Space characters (spaces and tabs)

cntrl

Control character

digit

Numeric character

graph

Character that can be displayed (excluding spaces)

lower

Lowercase character

print

Character that can be displayed (including spaces)

punct

Punctuation

space

Space character

upper

Uppercase character

xdigit

Hexadecimal number

[#*n]

This has the same meaning as specifying n characters specified in #, where each character is one byte. This format is used for character-string-2 in syntax 1. If n is omitted or if 0 is specified, the value of character-string-2 is assumed to have the same length as character-string-1, and characters exceeding that length are ignored. If the value of n begins with 0, the value is assumed to be an octal number. For other cases, the value is assumed to be a decimal number.

Return codes

Return code

Meaning

0

Normal termination

1 or greater

Error termination

Notes

Examples

Example 1: Character strings input from the standard input are replaced according to the specifications of character-string-1 and character-string-2. In this example, character strings of different types of brackets are unified as square brackets ([ ]).

Contents of file1

[apple],{banana}
[orange],<peach>,{cherry}

Command execution results

$ tr  '{}<>' '[][]' < file1
[apple],[banana]
[orange],[peach],[cherry]
Example 2: The complement of character string-1 is replaced with character-string-2, and then consecutive characters are compressed into one character. In this example, alphabetic characters are obtained from the character strings input from the standard input.

Contents of file1

[apple],{banana}
[orange],<peach>,{cherry}

Command execution results

$ tr -s -c '[:alpha:]' '[\n*]' < file1

apple
banana
orange
peach
cherry
Example 3: Character strings input from the standard input are deleted according to the specifications of character-string-1. In this example, brackets are deleted.

Contents of file1

[apple],{banana}
[orange],<peach>,{cherry}

Command execution results

$ tr -d '{}<>[]' < file1
apple,banana
orange,peach,cherry
Example 4: Character strings input from the standard input are replaced according to the specifications of character-string-1 and character-string-2, and then consecutive characters are compressed. In this example, brackets are replaced with commas (,) and then the commas (,) are compressed into one character.

Contents of file1

[apple],{banana}
[orange],<peach>,{cherry}

Command execution results

$ tr -s '{}<>[]' ','< file1
,apple,banana,
,orange,peach,cherry,
Example 5: Characters in character-string-1 are deleted from the character strings input from the standard input, and then consecutive characters are compressed according to character-string-2. In this example, parentheses are deleted and commas (,) are compressed.

Contents of file1

[apple],{banana}
[orange],<>,{cherry}

Command execution results

$ tr -ds '{}<>[]' ','< file1
apple,banana
orange,cherry
Example 6: character-string-1 is truncated to the same length as character-string-2, and then characters are replaced as specified. In this example, curly brackets ({ }) are replaced with square brackets ([ ]). Angle brackets (< >) are not replaced.

Contents of file1

[apple],{banana}
[orange],<peach>,{cherry}

Command execution results

$ tr -t '{}<>' '[]'< file1
[apple],[banana]
[orange],<peach>,[cherry]