tr(1) — Commands
OSF — Environment_Note_Added
NAME
tr − Translates characters
SYNOPSIS
tr [-Acds] string1 string2
The tr command copies characters from the standard input to the standard output with substitution or deletion of selected characters.
FLAGS
-ATranslates on a byte-by-byte basis. When you specify this flag, tr does not support extended characters.
-cComplements (inverts) the set of characters in string1 with respect to the universe of characters whose codes are 001 through 377 octal if you specify -A, and all characters if you do not specify -A.
-dDeletes all characters in string1 from output.
-sChanges characters that are repeated output characters in string2 into single characters.
DESCRIPTION
Input characters from string1 are replaced with the corresponding characters in string2. tr cannot handle an ASCII NUL (\000) in string1 or string2; it always deletes NUL from the input.
The trbsd command is a BSD compatible version of tr.
The following abbreviations can be used to introduce ranges of ASCII characters or repeated characters:
[a-z]Stands for a string of characters whose ASCII codes run from character a to character z, inclusive.
[a∗number]
Stands for number repetitions of a. number is considered to be in decimal unless the first digit of number is 0; then it is considered to be in octal.
Use the escape character \ (backslash) to remove the special meaning from any character in a string. Use the \ (backslash) followed by 1, 2, or 3 octal digits for the code of a character.
This version of tr does not support internationalized regular expression syntax. This means that if you specify a range of characters, tr only includes ASCII characters in that range, regardless of your current locale. (See grep for more information about internationalized regular expressions.) However, if you include literal extended characters in string1 or string2, tr processes them appropriately.
Japanese Language Support
You can use two octal sequences to specify a multibyte Kanji character. If you specify ranges of Kanji characters, they are interpreted for translation as a string of Kanji characters in ascending sequence in their binary representation.
EXAMPLES
1.To translate braces into parentheses, enter:
tr ’{}’ ’()’ <textfile >newfile
This translates each { (left brace) to a ( (left parenthesis) and each } (right brace) to ) (right parenthesis). All other characters remain unchanged.
2.To translate lowercase ASCII characters to uppercase, enter:
tr ’[a-z]’ ’[A-Z]’ <textfile >newfile
3.If the strings are not the same length, enter:
tr ’[0-9]’ ’#’ <textfile >newfile
This translates each 0 into a # (number sign); if the two character strings are not the same length, the extra characters in the longer one are ignored.
4.To translate each digit to a # (number sign), enter:
tr ’[0-9]’ ’[#∗]’ <textfile >newfile
The ∗ (asterisk) tells tr to repeat the # (number sign) enough times to make the second string as long as the first one.
5.To translate each string of digits to a single # (number sign), enter:
tr -s ’[0-9]’ ’[#∗]’ <textfile >newfile
6.To translate all ASCII characters that are not specified, enter:
tr -c ’[ -~]’ ’[A-_]?’ <textfile >newfile
This translates each nonprinting ASCII character to the corresponding control key letter (\001 translates to A, \002 to B, and so on). ASCII DEL (\177), the character that follows ~ (tilde), translates to a ? (question mark).
ENVIRONMENT NOTES
This section describes system features that are not generic to OSF/1 but that are provided in this OSF/1 implementation.
System V Compatibility
The root of the directory tree that contains the commands modified for SVID-2 compliance is specified in the file /etc/svid2_path. You can use /etc/svid2_profile as the basis for, or to include in, your .profile. The file /etc/svid2_profile reads /etc/svid2_path and sets the first entries in the PATH environment variable so that the modified SVID-2 commands are found first.
In the SVID-2 compliant version of the tr command, only characters in the octal range of 1 to 377 are complemented when you specify the -c option. This behavior is accomplished because the -A option is implicitly forced to be on when you specify the -c option.
RELATED INFORMATION
Commands: ed(1)/red(1), sh(1), trbsd(1).
Files: ascii(5).