Linux and Unix tr command
The tr command automatically translates (substitutes, or maps) one set of characters to another.
The tr utility copies the standard input to the standard output with substitution or deletion of selected characters.
tr [-Ccsu] string1 string2
In this form, the characters in the string 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.
tr [-Ccu] -d string1
In this form, the characters in string1 are deleted from the input.
tr [-Ccu] -s string1
In this form, the characters in string1 are compressed as described for the -s option (see below).
tr [-Ccu] -ds string1 string2
In the fourth form, the characters in string1 are deleted from the input, and the characters in string2 are compressed as described for the -s option.
|-C||Complement the set of characters in string1, that is "-C ab" includes every character except for 'a' and 'b'.|
|-c||Same as -C but complement the set of values in string1.|
|-d||Delete characters in string1 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.|
|-u||Guarantee that any output is unbuffered.|
How Characters Are Specified
When specifying the characters to translate with tr, the following conventions are used to represent sets (or "classes") of characters.
Any character not described by one of the following conventions simply represents itself.
|\octal||A backslash followed by 1, 2 or 3 octal digits represents a character with that encoded value. To follow an octal sequence with a digit as a character, pad the octal sequence on the left with zeroes.|
A backslash followed by certain special characters maps to special values:
|c-c||Character range. For non-octal range endpoints represents the range of characters between the range endpoints, inclusive and in ascending order, as defined by the collation sequence. If either or both of the range endpoints are octal sequences, it represents the range of specific coded values between the range endpoints, inclusive.|
|[:class:]||Represents all characters belonging to the defined character class. Class names are:
With the exception of case conversion, characters in the classes are in unspecified order.
For specific information as to which ASCII characters are included in these classes, see ctype and related manual pages.
"[=equiv=]" Represents all characters belonging to the same equivalence class as equiv, ordered by their encoded values.
|[#*n]||Represents n repeated occurrences of the character represented by #. This expression is only valid when it occurs in string2. If n is omitted, or is zero, it is be interpreted as large enough to extend string2 sequence to the length of string1. If n has a leading zero, it is interpreted as an octal value, otherwise, it is interpreted as a decimal value.|
The LANG, LC_ALL, LC_CTYPE and LC_COLLATE environment variables affect the execution of tr.
tr returns an exit status of 0 if it operated successfully, and a value greater than zero if an error occurred.
tr -cs "[:alpha:]" "\n" < file1
Create a list of the words in file1, one per line, where a word is taken to be a maximal string of letters.
tr "[:lower:]" "[:upper:]" < file1
Translate the contents of file1 to uppercase.
tr -cd "[:print:]" < file1
Remove all non-printable characters from file1.
tr "[=e=]" "e"
Remove all "diacritical" marks from accented versions of the letter e.