9.2 tr
The command tr allows us to either translate, delete or squeeze output character by character. There are many operations available when using the tr command such as searching and replacing text, transforming string from uppercase to lowercase (or vice versa), removing repeated characters from the string etc.
Translating (=replacing) characters: tr
Translating is the default behavior of tr. It expects two list of characters and translates those from the first with those from the second list. To translate all lower case i to upper case I:
If multiple characters are provided, the translation is in order. To translate all lower case i with I and all e with E:
Deleting characters: tr -d
With this option, tr simply removes all occurences of the characters provided. To remove all white space, for instance, you can use:
If multiple characters are provided, deletion applies to each of them individually. It never loks for “words”.
In this case, for instance, we deleted every letter i and e, not the word “ie”.
Squeezing characters: tr -s
You can also use tr to squeeze characters with the -s option. Squeezing means that you take a sequence of identical characters and you reduce their number to just 1.
In the first example we squeezed the … in the end into a single ‘.’, and in the second example we added a space as well.
You can also use tr to convert lower case to upper case: