tr (Unix)

from Wikipedia, the free encyclopedia

tr is a Unix command , whose name is an abbreviation for translate ( German: translate), the specific characters replaced from a data stream or removed.

The tool reads the data stream of the standard input , writes to the standard output and, depending on the mode, requires one (delete and compress) or two (replace) arguments.

If characters are to be replaced, two arguments are required, first the characters to be replaced, and the second the new ones.

Example:

jimbo@thinkpad:~$ echo Meyer | tr y i
Meier

Multiple replacements of individual characters are possible at once. E.g. replaced

tr 'abcd' 'jkmn'

all occurring a through j , b through k etc.

Characters following one another in the alphabet can be specified with a hyphen:

tr 'a-d' 'jkmn'

With the operator s all consecutive identical characters are replaced by a single one. Example:

jimbo@thinkpad:~ echo muuuuh | tr -s u
muh

The operand d deletes all characters specified in the first argument

tr -d '\r'

where \ r stands for a carriage return (byte value 13). With this command, this break character, which is not used under Unix, is removed without replacement.

If a c is given, the reverse applies, i.e. here

tr -cd '[:alnum:]'

where the expression [: alnum:] stands for all alphanumeric characters. This removes all non-alphanumeric characters.

See also

Web links