Translating with tr
tr is a small and beautiful command in the Unix command-warrior toolkit. It is one of the important commands frequently used to craft beautiful one-liner commands. It can be used to perform substitution of characters, deletion of the characters, and squeezing of repeated characters from the standard input. It is often called translate
, since it can translate 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
tr accepts input only through stdin (standard input) and cannot accept input through command-line arguments. It has the following invocation format:
tr [options] set1 set2
Input characters from stdin are mapped from set1 to set2 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, or else, if the length of set2 is greater...