Translates characters.
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:
Item | Description |
---|---|
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:
Except for [:lower:] and [:upper:] conversion character classes, the characters specified by other character classes are placed in the array in an unspecified order. Because the order of the characters specified by character classes is undefined, the characters should be used only if the intent is to map several characters into one. An exception to this is the case of conversion character classes. 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:
|
\\ | 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.
Item | Description |
---|---|
-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. If the -d option is not specified, the complements of the characters specified by String1 will be placed in the array in ascending collation sequence as defined by the current setting of LC_COLLATE. |
-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. If the -d option is not specified, the complement of the values specified by String1 will be placed in the array in ascending order by binary value. |
-d | Deletes each character from standard input that is contained
in the string specified by String1. Note:
|
-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. |
This command returns the following exit values:
Item | Description |
---|---|
0 | All input was processed successfully. |
>0 | An error occurred. |
tr '{}' '()' < textfile > newfile
This
translates each { (left brace) to ( (left parenthesis)
and each } (right brace) to ) (right parenthesis).
All other characters remain unchanged.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.tr 'a-z' 'A-Z' < textfile > newfile
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.tr -d '\0' < textfile > newfile
tr -s '\n' < textfile > newfile
OR
tr -s '\012' < textfile > newfile
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. tr -s '[:space:]' '[#*]'