From the official description, it’s easy to understand the value of this tool. Linux comes up with a big collection of built-in tools. There are some special ones that are super useful for text manipulation. We’ve already covered a number of those, like Vim, Nano, awk, sed and other tools.
Those who work with texts on a regular basis, “tr” is definitely going to be really, really useful. This article will illustrate the most common usage of “tr” with sufficient examples.
Note: The tutorial is going to use a ton of dummy files with random content. All the random strings are generated by Random.org.
This is the full path of the “tr” command tool.
For using “tr” tool, you have to use the following command structure.
There are different options and ways to manipulate the texts using “tr”. At first, let’s check out this demo file.
All the characters are in lowercase, right? Let’s transform them to uppercase!
Here, the first parameter of “tr” is indicating to perform a translation on all the lowercase characters of the input. The second part is telling to transform them into uppercase at the output.
Now, let’s do the opposite.
There’s also another way of performing this same task. Let’s check it out.
Now, instead of telling “tr” to translate uppercase to lowercase or lowercase to uppercase, we told to identify entries matching the range “a” to “z” and translate into their equivalent from the range “A” to “Z”.
This similar method can also be used to translate uppercase to lowercase.
Playing with numbers
We saw how to translate uppercase to lowercase, right? It’s time to play around with digits.
Using the following command, we can easily translate all the digits (0-9) into their equivalent characters!
Awesome! How about uppercase ones?
Simple, yet interesting, right? We can also transform characters to digits as well!
Uh oh, seems like things broke down, right? Well, we can only use ‘a’ to ‘j’ to represent single digits. If there’s any character that’s out of this bound, “tr” will replace the character with ‘]’.
As the official description suggests, “tr” can do more than just translation of characters. In the following example, we’ll check out how to use “tr” to delete certain characters.
Now, let’s delete all the numbers from the content.
Here, the “-d” flag is to tell “tr” to delete and [0-9] denoting digits to delete.
We can also do that with the characters. The following command will keep all the numbers but remove all the characters.
How about removing just a single specific character from the file?
It deleted all the ‘y’ entries from the file.
Squeezing repeated characters
There are times when a character is repeated sequentially. If you’re dealing with such annoying issues, just let “tr” take care of it! Using the following command, you can squeeze such occurrences. It essentially keeps the initial occurrence of the character and deletes the additional ones.
First, time to check how the demo file looks.
Now, pass the content to “tr”.
Here, the “-s” parameter is the indicator for performing the “squeeze” action.
Let’s check out the demo file.
It contains a sentence with spaces dividing the words, right? Let’s split the words into new lines.
This command replaced all the space characters with newline characters.
Let’s merge the broken sentence into a long sentence again.
Just like we translated characters before, this time, we’ll do the same but with a single character only.
In this file, let’s change all the newline characters into a semicolon.
I had to wrap it around “echo”. Otherwise, it would create some output because the last newline character would also be translated into a semicolon.
There’s a different way of changing characters. However, this one is harder to control.
Wow! Let’s wrap it around “echo” to better understand the output.
What happened here? Using the “-c” flag, “tr” will only keep the target character unchanged. In the case of mismatch, every other character will be transformed. Here, any character other than ‘b’ was replaced by ‘z’.
“tr” can also work with strings. Let’s perform string replacement.
My string to replace is shorter than the string to be replaced with, so it didn’t fit.
By now, you’ve noticed that there are a number of character sets supported by “tr”. While a number of them were used in the above examples, other characters sets are also quite helpful. Here’s a list of all the supported character lists.
POSIX character sets
- [:digit:] : Digits 0-9
- [:alpha:] : Alphabets a-z and A-Z.
- [:alnum:] : Alpha-numeric characters
- [:punct:] : Punctuation symbols
- [:space:] : Any whitespace character, for example, space, tab, FF, CR, NL, FF, etc.
- [:upper:] : All uppercase alphabets
- [:lower:] : All lowercase alphabets
- [:cntrl:] : All control characters (NL, CR, EXT, STX, ACK, SO, DC1, DC2, NAK, ETB, ESC, IS1, IS2, DEL etc.)
Additional character sets
- [A-Z] : All uppercase alphabets
- [a-z] : All lowercase alphabets
- [0-9] : All digits
There are tons of ways all these features of “tr” can benefit the users. I always recommend to check out all the available options and in-depth guides on any Linux tool from their man, info and help pages as they can offer more valuable knowledge.