Translating with tr
The tr
command is a versatile tool in the Unix command–warrior's kit. It is used to craft elegant one-liner commands. It performs substitution of characters, deletes selected characters, and can squeeze repeated characters from the standard input. Tr is short for translate, since it translates a set of characters to another set. In this recipe, we will see how to use tr
to perform basic translation between sets.
Getting ready
The tr
command accepts input through stdin (standard input) and cannot accept input through command-line arguments. It has this invocation format:
tr [options] set1 set2
Input characters from stdin
are mapped from the first character in set1
to the first character in set2
, and so on and the output is written to stdout
(standard output). set1
and set2
are character classes or a set of characters. If the length of sets is unequal, set2
is extended to the length of set1
by repeating the last character; otherwise if the length of set2
is greater than that...