Translate characters (POSIX)
tr [-cs] [-r filename] string1 string2 tr [-cs] [-r filename] string1 tr -d [-c] [-r filename] string1 tr -ds [-c] [-r filename] string1 string2
If you don't specify string2, tr squeezes instances of the characters in string1 to a single instance of that character.
If you specify both -d and -s, tr deletes instances of the characters in string1 and squeezes instances of the characters in string2 (i.e. tr doesn't translate in this case). |
The tr utility copies the standard input to the standard output with substitution or deletion of selected characters. The options specified and the string1 and string2 operands control translations that occur while copying characters.
The default behavior is to replace each input character found in string1 with the character at the same position in string2, while copying characters not in string1 unchanged.
When string2 is shorter than string1, string2 is extended to the length of string1 by duplicating the last character of string2. If string2 is explicitly a string of zero length, it's padded with NUL characters.
The string1 and string2 operands often require quoting to avoid interpretation by the shell. Single quotes are usually the proper quoting mechanism. |
Use the following conventions in string1 or string2 or both to specify characters, character ranges, character classes, or collating elements:
alnum, alpha, blank, cntrl, digit, graph, lower, print, punct, space, upper, xdigit
Convert all lowercase characters in the input to the corresponding uppercase characters:
tr '[:lower:]' '[:upper:]' <file1 >file2
Or
tr '[a-z]' '[A-Z]' <file1 >file2
Create a list of all words in file1 one per line in file2 where a word is taken to be a maximal string of letters (octal 012 is the code for newline):
tr -cs '[:alpha:]' '[\012*]' <file 1 >file2
Convert a DOS file into a UNIX file:
tr -d '\15' <infile >outfile