[ Bottom of Page | Previous Page | Next Page | Contents | Index | Library Home | Legal | Search ]

Commands Reference, Volume 5

tr Command

Purpose

Translates characters.

Syntax

tr-c -cds -cs | -ds -s ] [  -A ] String1 String2

tr { -cd -cs -d -s } [  -A ] String1

Description

The tr command deletes or substitutes characters from standard input and writes the result to standard output. The tr command performs three kinds of operations depending on the strings specified by the String1 and String2 variable and on the flags specified.

Transforming Characters

If String1 and String2 are both specified and the -d flag is not specified, the tr command replaces each character contained in String1 from the standard input with the character in the same position in String2.

Deleting Characters Using the -d Flag

If the -d flag is specified, the tr command deletes each character contained in String1 from standard input.

Removing Sequences Using the -s Flag

If the -s flag is specified, the tr command removes all but the first character in any sequence of a character string represented in String1 or String2. For each character represented in String1, the tr command removes all but the first occurrence of the character from standard output. For each character represented in String2, the tr command removes all but the first occurrence in a sequence of occurrences of that character in the standard output.

Special Sequences for Expressing Strings

The strings contained in the String1 and String2 variables can be expressed using the following conventions:

C1-C2 Specifies the string of characters that collate between the character specified by C1 and the character specified by C2, inclusive. The character specified by C1 must collate before the character specified by C2.
Note
The current locale has a significant effect on results when specifying subranges using this method. If the command is required to give consistent results irrespective of locale, the use of subranges should be avoided.
[C*Number] Number is an integer that specifies the number of repetitions of the character specified by C. Number is considered a decimal integer unless the first digit is a 0; then it is considered an octal integer.
[C*] Fills out the string with the character specified by C. This option, used only at the end of the string contained within String2, forces the string within String2 to have the same number of characters as the string specified by the String1 variable. Any characters specified after the * (asterisk) are ignored.
[ :ClassName: ] Specifies all of the characters in the character class named by ClassName in the current locale. The class name can be any of the following names:

alnum      lower
alpha      print
blank      punct
cntrl      space
digit      upper
graph      xdigit

For more information on character classes, see the ctype subroutines.

[ =C= ] Specifies all of the characters with the same equivalence class as the character specified by C.
\Octal Specifies the character whose encoding is represented by the octal value specified by Octal. Octal can be a one-, two- or three-digit octal integer. The NULL character can be expressed with '\0', and is processed like any other character.
\ControlCharacter Specifies the control character that corresponds to the value specified by ControlCharacter. The following values can be represented:
\a
Alert
\b
Backspace
\f
Form-feed
\n
New line
\r
Carriage return
\t
Tab
\v
Vertical tab
\\ Specifies the \ (backslash) as itself, without any special meaning as an escape character.
\[ Specifies the [ (left bracket) as itself, without any special meaning as the beginning of a special string sequence.
\- Specifies the - (minus sign) as itself, without any special meaning as a range separator.

If a character is specified more than once in String1, the character is translated into the character in String2 that corresponds to the last occurrence of the character in String1.

If the strings specified by String1 and String2 are not the same length, the tr command ignores the extra characters in the longer string.

Flags

-A Performs all operations on a byte-by-byte basis using the ASCII collation order for ranges and character classes, instead of the collation order for the current locale.
-c Specifies that the value of String1 be replaced by the complement of the string specified by String1. The complement of String1 is all of the characters in the character set of the current locale, except the characters specified by String1. If the -A and -c flags are both specified, characters are complemented with respect to the set of all 8-bit character codes. If the -c and -s flags are both specified, the -s flag applies to characters in the complement of String1.
-d Deletes each character from standard input that is contained in the string specified by String1.
-s Removes all but the first in a sequence of a repeated characters. Character sequences specified by String1 are removed from standard input before translation, and character sequences specified by String2 are removed from standard output.
String1 Specifies a string of characters.
String2 Specifies a string of characters.

Exit Status

This command returns the following exit values:

0 All input was processed successfully.
>0 An error occurred.

Examples

  1. To translate braces into parentheses, type:

    tr '{}' '()' < textfile > newfile

    This translates each { (left brace) to ( (left parenthesis) and each } (right brace) to ) (right parenthesis). All other characters remain unchanged.

  2. To translate braces into brackets, type:

    tr '{}' '\[]' < textfile > newfile

    This translates each { (left brace) to [ (left bracket) and each } (right brace) to ] (right bracket). The left bracket must be entered with a \ (backslash) escape character.

  3. To translate lowercase characters to uppercase, type:

    tr 'a-z' 'A-Z' < textfile > newfile
  4. To create a list of words in a file, type:

    tr -cs '[:lower:][:upper:]' '[\n*]' < textfile > newfile

    This translates each sequence of characters other than lowercase letters and uppercase letters into a single newline character. The * (asterisk) causes the tr command to repeat the new line character enough times to make the second string as long as the first string.

  5. To delete all NULL characters from a file, type:

    tr -d '\0' < textfile > newfile
  6. To replace every sequence of one or more new lines with a single new line, type:

    tr -s '\n' < textfile > newfile

    OR

    tr -s '\012' < textfile > newfile
  7. To replace every nonprinting character, other than valid control characters, with a ? (question mark), type:

    tr -c '[:print:][:cntrl:]' '[?*]' < textfile > newfile

    This scans a file created in a different locale to find characters that are not printable characters in the current locale.

  8. To replace every sequence of characters in the <space> character class with a single # character, type:

    tr -s '[:space:]' '[#*]'

Related Information

The ed command, trbsd command.

The ctype subroutines.

National Language Support Overview in AIX 5L Version 5.2 National Language Support Guide and Reference.

[ Top of Page | Previous Page | Next Page | Contents | Index | Library Home | Legal | Search ]