How to Use Linux TR Command with Examples

The tr (translate) command is used in Linux mainly for translating and deleting characters. It can be used to convert uppercase to lowercase, squeeze repeating characters and deleting characters.

Tr command requires two sets of characters for transformations and also can be used with other commands using Unix pipes for advanced translations.

In this tutorial, we learn how to use tr command in Linux with some examples.

tr command and syntax

Tr command uses the following syntax which requires two sets of characters to action.

tr [options] "set1" "set2"
echo "something to translate" | tr  "set1" "set2"
tr "set1" "set2" < file-to-translate
tr "set1" "set2" < file-to-translate > file-output

1) Convert lower case to upper case

We can use tr command to convert sentences or words from lower case to upper case or vise versa.

You can either [:lower:] [:upper:] or "a-z" "A-Z" to convert lower case to upper case.

The following examples illustrate different options available:

$sudo echo "change me to capital letter" | tr [:lower:] [:upper:] 
CHANGE ME TO CAPITAL LETTER

Here we convert the contents of a file and print the result only in the standard output of the console.

$sudo cat file 
Can you see how tr command can be wonderful?

$sudo tr "a-z" "A-Z" < file 
CAN YOU SEE HOW TR COMMAND CAN BE WONDERFUL?

$sudo cat file
Can you see how tr command can be wonderful?

In the following, the contents in the 'file' will be converted to upper case and saved to a file called 'output'

$sudo tr "a-z" "A-Z" < file > output

#sudo cat output
CAN YOU SEE HOW TR COMMAND CAN BE WONDERFUL?

2) Removes characters

The -d option is used to remove all occurrences of characters that have been specified. Lets check different -d options with examples.

The following command will remove all occurrences of characters 'cawe' from the first set.

$sudo echo "Can you see how tr command can be wonderful?" | tr -d "cawe?" 
Cn you s ho tr ommnd n b ondrful

The following command will  remove all digits from a sentence:

Note [:digit:] stands for all digit characters.

$sudo echo "Remove my id number 567893" | tr -d [:digit:]

Output

Remove my id number

The below command will remove newlines from a text file

$sudo tr -d '\n' < file1.txt
or
$sudo tr -d '\012' < file1.txt

3) Remove non-matching characters (complement)

With -c option you can replace the non-matching characters with another set of characters.

In the following example, it replaces all the non-matching characters 'bc123d56E' with 't'.

$sudo echo "Abc123d56E" | tr -c 'A' 't'
Atttttttttt

4) Translate white space to tabs

We can use tr command to translate all white space to tabs.

Check the following example

$sudo echo "I need more space" | tr [:space:] '\t'
Output
I       need    more    space

5) Squeeze repetition of characters

With -s option we can squeeze repeated characters.

In the following examples, the repeated spaces (continuous) is converted to a single space.

$sudo echo "I am so        wide" | tr -s " "
I am so wide

or

$sudo echo "I am so        wide" | tr -s '[:space:]' '#'
I am so wide

Here we remove continuous spaces and place character '#' in all single spaces.

sudo echo "I am so            wide" | tr -s '[:space:]' '#'
I#am#so#wide#

6) Translates to a single newline

The following translates each sequence of space into a single newline character.

Note [:alpha:] stands for all letters.

$sudo cat file
Can you see how tr command can be wonderful

$sudo cat file | tr -s "[:alpha:]" "\n"
Can
you
see
how
tr
command
can
be
wonderful
$

7) Generate a list of unique words from a file

This is very useful practical example where we can use tr to generate unique words from a file.

$sudo cat file
unique1 unique1 unique2 unique3 unique4 unique4
$sudo cat file | tr -cs "[:alnum:]" "\n" | sort | uniq -c | sort -rn
      2 unique4
      2 unique1
      1 unique3
      1 unique2
$

8) Encode letters using ROT

ROT (Caesar Cipher) is a kind of cryptography wherein encoding is done by moving the letters in the alphabet to its next letter.

Lets check how we can use tr command for encrypting.

In the following example each character in the first set will be replaced with the corresponding character in the second set. The first set specified is [a-z], which means abcdefghijklmnopqrstuvwxyz. The second is [n-za-m], which turns into pqrstuvwxyzabcdefghijklmn.

tr 'a-z' 'p-za-n'

Simple command to shows above theory:

$sudo echo 'abg' | tr 'ab' 'ef'
Output
efg

This is very useful when you need to encrypt an email. Check below

$sudo echo 'cryptography@example.com' | tr 'A-Za-z' 'N-ZA-Mn-za-m'

Output

pelcgbtencul@rknzcyr.pbz

Conclusion

It is a very powerful linux command when using with unix pipes and you can always refer tr command man pages for more information. If you have any questions or feedback, feel free to leave a comment.

Bobbin Zachariah 12:01 pm

Comments

Your email address will not be published. Required fields are marked *

All comments are subject to moderation.