tr - Translate
characters
Synopsis
tr [-cs] string1 string2
tr [-c] -d string1
tr [-c] -s string1
tr [-c] -ds string1 string2
Description
The tr utility copies the standard input to the
standard output with substitution or deletion of selected
characters.
In the first synopsis form, the characters in string1
are translated into the characters in string2 where the
first character in string1 is translated into the first
character in string2 and so on. If string1 is
longer than string2, the last character found in
string2 is duplicated until string1 is exhausted.
In the second synopsis form, the characters in string1
are deleted from the input.
In the third synopsis form, the characters in string1
are compressed as described for the -s option
below.
In the fourth synopsis form, the characters in string1
are deleted from the input, and the characters in string2
are compressed as described for the -s option
below.
The following conventions can be used in string1 and
string2 to specify sets of characters. Any character not
described by one of the following conventions represents
itself.
- nnn
- A backslash (\) followed by 1, 2 or 3 octal digits represents a
character with that encoded value.
- char
- To follow an octal sequence with a digit as a character, left
zero-pad the octal sequence to the full 3 octal digits. A backslash
(\) followed by certain special characters maps to special values.
The special characters and their values are:
- a - alert character
- b - backspace
- f - form-feed
- n - newline
- r - carriage return
- t - tab
- v - vertical tab
- A backslash (\) followed by any other character maps to that
character.
- c-c
- Represents the range of characters between the range endpoints,
inclusively.
- [:class:]
- Represents all characters belonging to the defined character
class. These are the class names:
- alnum - alphanumeric characters
- alpha - alphabetic characters
- cntrl - control characters
- digit - numeric characters
- graph - graphic characters
- lower - lower-case alphabetic characters
- print - printable characters
- punct - punctuation characters
- space - space characters
- upper - upper-case characters
- xdigit - hexadecimal characters
Note: |
With the exception of the upper and lower classes, characters
in the classes are in unspecified order. In the upper and lower
classes, characters are entered in ascending order. |
Options
- -c
- Complement the set of characters in string1, that is
-c ab includes every character except for "a" and
"b".
- -d
- Delete characters from the input.
- -s
- Squeeze multiple occurrences of the characters listed in the
last operand (either string1 or string2) in the
input into a single instance of the character. This occurs after
all deletion and translation is completed.
Exit Status
- 0 on success
- >0 if an error occurs.
Related information
Examples
- Create a list of the words in file1, one per line, where a word
is taken to be a maximal string of letters.
tr -cs '[:alpha:]' 'n' < file1
- Translate the contents of file1 to upper-case.
tr '[:lower:]' '[:upper:]' < file1
tr 'a-z' 'A-Z' < file1
- Remove the non-printable characters from file1.
tr -cd '[:print:]' < file1