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 the Linux operating systems through some examples.
tr command and syntax
Tr command uses the following syntax which requires two sets of characters to action.
tr [options] "SET1" "SET2"
where SET's are a group of characters. You use interpreted sequences, listed some of them.
\NNN -> character with octal value NNN (1 to 3 octal digits)
\\ -> backslash
\n -> new line
\r -> return
\t -> horizontal tab
[:alnum:] -> all letters and digits
[:alpha:] -> all letters
[:blank:] -> all horizontal whitespace
[:cntrl:] -> all control characters
[:digit:] -> all digits
[:lower:] -> all lower case letters
[:upper:] -> all upper case letters
The following variations of tr syntax can be applied:
echo "something to translate" | tr "SET1" "SET2"
tr "SET1" "SET2" < file-to-translate
tr "SET1" "SET2" < file-to-translate > file-output
Tr options are:
--complement -> Complement the character set in 'SET1"
--delete -> Delete characters in SET1.
--squeeze-repeats -> Replace each input sequence of a repeated character that is listed in SET1 with a single occurrence of that character.
1) Convert lower case to upper case
We can use tr for case conversion, ie 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 convert the characters from lower case to upper case and print results on the standard output.
$ sudo echo "change me to capital letter" | tr [:lower:] [:upper:] CHANGE ME TO CAPITAL LETTER
This example translates the contents of a file named 'input.txt' and prints the result only in the standard output of the console.
$ sudo cat input.txt Can you see how command can be wonderful? $ sudo tr "a-z" "A-Z" < input.txt CAN YOU SEE HOW COMMAND CAN BE WONDERFUL? $ sudo cat input.txt Can you see how command can be wonderful?
In the following, the contents in the 'input.txt' will be converted to upper case and saved to a file called 'output.txt'
$ sudo tr "a-z" "A-Z" < input.txt > output.txt $ sudo cat output.txt CAN YOU SEE HOW COMMAND CAN BE WONDERFUL?
Note: The sed command has the
y option that works like tr, that replace all occurrences of characters in 'set1' with the corresponding characters in 'set2'.
2) Removes characters
-d option is used to remove all occurrences of characters that have been specified. Let's 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 command can be wonderful?" | tr -d "cawe?" Cn you s ho ommnd n b ondrful
The following command will remove all digits from a sentence:
[:digit:] stands for all digit characters.
$ sudo echo "Remove my id number 567893" | tr -d [:digit:]
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)
-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
Another real-time example where we want to extract only digits from a set of characters:
$ echo "My year of birth is 1975" | tr -cd [:digit:]
4) Translate white space to tabs
We can have tr command to translate all white space to tabs, apply tr with
Check the following example
$ sudo echo "I need more space" | tr [:space:] '\t'
Output I need more space
5) Squeeze repetition of characters
-s option we can squeeze repeated occurrence of 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
$ 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.
[:alpha:] stands for all letters.
$ sudo cat file Can you see how command can be wonderful $ sudo cat file | tr -s "[:alpha:]" "\n" Can you see how command can be wonderful $
7) Generate a list of unique words from a file
This is a 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 type of cryptography wherein encoding is done by moving the letters in the alphabet to its next letter.
Let's check how to use tr 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
tr 'a-z' 'p-za-n'
Simple command to shows the 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 'email@example.com' | tr 'A-Za-z' 'N-ZA-Mn-za-m'
It is a very powerful linux command when using Unix pipes and very commonly used in the shell script.
You can always refer man page for more information about this command line utility. If you have any questions or feedback, feel free to leave a comment.