Home Linux Commands Tr Command In Linux Explained With Examples

Tr Command In Linux Explained With Examples

By Karthick
1,670 Views

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 tr command.

$ man tr
$ tr --help

The 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"
OSTechnix
Convert lower case to upper case using tr command
Convert lower case to upper case using tr command

Example 2 - Character sets and special characters

The tr command offers the usage of character sets which makes the conversion very easy. The tr command can also interpret special characters like (\n,\t,\v) which can be translated with different characters.

Character sets
Character sets

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 (.) using "[:punct:]" character set.

$ echo "Ubuntu 20.04 #$%^" | tr "[:punct:]" "."
Translate using character set
Translate using character set

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"
OSTECHNIX
Uppercase conversion
Uppercase conversion

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
OSTECHNIX
$ echo "OSTECHNIX" | tr "A-Z" "a-z" # Upper to Lower case
ostechnix
Lower and Upper case conversion using ranges
Lower and Upper case conversion using ranges

The character classes can also be used instead of the above two methods.

$ echo "OSTECHNIX" | tr "[:upper:]" "[:lower:]"
ostechnix
$ echo "ostechnix" | tr "[:lower:]" "[:upper:]"
OSTECHNIX
Conversion using character set
Conversion using character set

Example 4 - Delete characters

There are a set of optional arguments that tr supports to perform various actions.

One such flag is "-d or --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
Remove white spaces
Remove white spaces

If you wish to remove both space and tabs (horizontal whitespaces), you can use the "[:blank:]" character set.

$ tr -d "[:blank:]" < tr_test.txt
Remove spaces and tabs
Remove spaces and tabs

You can also use the "[:space:]" character set to delete horizontal and vertical whitespaces.

$ tr -d "[:space:]" < tr_test.txt
Remove horizontal and vertical spaces
Remove horizontal and vertical spaces

Example 5 - Squeeze repeats

When the flag "-s or --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
Squeeze characters
Squeeze characters

Below is another example where newline characters (\n) are squeezed.

$ cat tr_test.txt | tr -s "\n"
Squeeze new line characters
Squeeze new line characters

Example 6 - Complement

When the -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:]"
Complement flag
Complement flag

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

Redirecting output to a file
Redirecting output to a file

Conclusion

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 sed, awk, etc. But knowing these commands and different ways will help you decide the right situation to use the right command/program.

You May Also Like

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More