The real power of Linux is there are many programs/utilities created to make our lives easy. When you write a lot of shell scripts in your environment this set of programs comes in handy, where you will use these programs to manipulate your input or output in your scripts. One such utility is
tr, which stands for translate. As the name implies, the tr command is used to translate, squeeze, and delete characters from standard input, writing to standard output in Linux and Unix-like operating systems.
In this article, we will learn tr command usage with practical examples. At the end of this guide, you will be comfortable in using the tr command and you will come to know in which scenario using this command would be beneficial.
tr command syntax and help
To know the syntax and list of arguments that tr command supports, refer the man page or use
--help flag with
$ man tr
$ tr --help
tr command accepts two arguments in the form of SET1 and SET2 where SET1 will be input characters which will be translated by characters in SET2. When certain flags are passed to the tr command, SET2 might be optionally used.
tr [OPTION]… SET1 [SET2]
Example 1 - Translate with tr command
Below is a simple example of how to use the
tr command. The
tr command is piped with the
echo command and lowercase "ost" is converted to uppercase "OST".
The conversion here will happen based on the position of characters in two sets. The first character in SET1 will be replaced by the first character in SET2 (o->O), similarly for all the characters (s->S,t->T).
$ echo "ostechnix" | tr "ost" "OST"
Example 2 - Character sets and special characters
tr command offers the usage of character sets which makes the conversion very easy. The
tr command can also interpret special characters like (
\v) which can be translated with different characters.
Let’s look at how to use the character set. In the below example, I have strings, Integers and punctuation characters. punctuation characters are replaced by dot (
"[:punct:]" character set.
$ echo "Ubuntu 20.04 #$%^" | tr "[:punct:]" "."
Example 3 - Case conversion
When working with strings, you may wish to change the case of the string from lower to upper or vice versa. Using the
tr command, you can do case conversion in different ways.
In the first way, you can type a to z in both set 1 and set 2.
$ echo "ostechnix" | tr "abcdefghijklmnopqrstuvwxyz" "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Please note that this is not a preferred way.
Instead of typing all the characters, you can also use ranges (i.e. a-z, A-Z).
$ echo "ostechnix" | tr "a-z" "A-Z" # Lower to Upper case
$ echo "OSTECHNIX" | tr "A-Z" "a-z" # Upper to Lower case
The character classes can also be used instead of the above two methods.
$ echo "OSTECHNIX" | tr "[:upper:]" "[:lower:]"
$ echo "ostechnix" | tr "[:lower:]" "[:upper:]"
Example 4 - Delete characters
There are a set of optional arguments that
tr supports to perform various actions.
One such flag is "
--delete" which will delete characters that are given in SET1.
Below is the input where I have three IPs with white spaces in each line and tabs (2) in the third line. Now using the
-d flag you can clean the white spaces.
192.168. 1. 12 192.168. 1. 13 192.168. 1. 14
Run the below command which will delete all the whitespaces. An important point to note here is tabs are not removed but only whitespace are removed.
$ tr -d " " < tr_test.txt
If you wish to remove both space and tabs (horizontal whitespaces), you can use the
"[:blank:]" character set.
$ tr -d "[:blank:]" < tr_test.txt
You can also use the
"[:space:]" character set to delete horizontal and vertical whitespaces.
$ tr -d "[:space:]" < tr_test.txt
Example 5 - Squeeze repeats
When the flag "
--squeeze-repeats" is used, the repeated characters given in SET1 will be replaced with a single occurrence of the character. In the below example, you can see I have used 4 equal to (
=) sign. Now
-s will replace 4 (
=) with 1 (
$ echo "Production IP ==== 192.168.1.10" | tr -s "=" Production IP = 192.168.1.10
Below is another example where newline characters (
\n) are squeezed.
$ cat tr_test.txt | tr -s "\n"
Example 6 - Complement
-c flag is used, it will do an inverse translation for the given set. In the below example the tr will translate all alphabets to
"*" normally. But when
-c is used, the operation will be inverted. Other than alphabets all other characters will be replaced with
$ echo "Employee Number is 1267890" | tr -c "[:alpha:]" "*"
You can combine this with other flags to get certain results. For example when you combine with the
-d flag, it will do an inverse delete operation.
$ echo "Employee Number is 1267890" | tr -cd "[:digit:]"
Note: From the above example, you can see the newline character is also translated/deleted. When using this method keep note of this point and proceed.
Example 7 - Redirect output
In all the previous sections, we read the input from the file and done some processing with the
tr command and the output is sent to the terminal. If you wish to redirect the output to a file instead of a terminal then use the redirection operator.
$ tr -s "\n" < tr_test.txt > new_tr_test.txt
$ cat new_tr_test.txt
In this article, I have walked you through how to use the
tr command. In Linux, there are always multiple ways to achieve the same thing. Whatever we have shown in this article can also be achieved with other programs like
awk, etc. But knowing these commands and different ways will help you decide the right situation to use the right command/program.