The tr command is used in Linux for translating characters, it needs two sets of characters. It replaces occurrences of the characters in the first set with the corresponding character from the second set. For example, tr command can be used to change small letters to capital letters and likewise.
When the strings are of the same length, then the corresponding character in the set2 is substituted for the occurrence of each character in set1. If set1 is longer than set2, then tr command will assign the last character of set2 to each of the unspecified positions past the end of the specified string.
Table of Contents
tr command and its syntax
To start let's see few syntax for tr command.
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) Use tr command to change case
Translate the word ‘welcome’ to upper-case:
# echo "welcome to linoxide" | tr [:lower:] [:upper:] WELCOME TO LINOXIDE
Take a file in entry and print the result only in the standard output of the console. Our file is not affected
# cat file Can you see how tr command can be wonderful? # tr "a-z" "A-Z" < file CAN YOU SEE HOW TR COMMAND CAN BE WONDERFUL? # cat file Can you see how tr command can be wonderful?
Take a file in entry and print the result in another file to save it. Our original file is not affected and we saved the result.
# tr "a-z" "A-Z" < file > output # cat output CAN YOU SEE HOW TR COMMAND CAN BE WONDERFUL?
2) Removes occurrence of characters and delete NULL characters
-d option removes all occurrences of characters that have been specified:
# echo "Can you see how tr command can be wonderful?" | tr -d "cawe?" Cn you s ho tr ommnd n b ondrful
# echo "Abc123d56E" | tr -d '[[:digit:]]' AbcdE
To delete all NULL characters from a file, you can do
tr -d '\0' < file > output
3) Replace the non-matching characters with another set of characters
-c option is used to replace the non-matching characters with another set of characters. complement set1 with respect to the universe of characters whose ASCII codes are 01 through 0377 octal.
# echo "Abc123d56E" | tr -c 'A' 't' Atttttttttt
4) Replace sequences of the same character
-s option converts each sequence of repeated newlines to a single newline, i.e. replace multiple spaces.
# echo "Can you see how tr command can be wonderful?" | tr -s " " "/" Can/you/see/how/tr/command/can/be/wonderful?
#echo "Can you see how tr command can be wonderful? | tr -s '[:space:]' '#' Can#you#see#how#tr#command#can#be#wonderful?#
5) Translates each sequence of characters into a single newline character
Translates each sequence of characters other than lowercase uppercase letters into a single newline character. The * (asterisk) causes the tr command to repeat the new line character enough times to make the second string as long as the first string.
# cat file | tr -cs "[:alpha:]" "\n" Can you see how tr command can be wonderful
6) Create a list of the unique words contained in the file one per line
# cat file | tr -cs "[:alnum:]" "\n" | sort | uniq -c | sort -rn 1 you 1 wonderful 1 tr 1 see 1 how 1 command 1 Can 1 can 1 be
7) Sort alphabetically a result obtain with tr command
Below command will list all the users listed in the access log and then sort them alphabetically.
# cat /var/log/squid/access.log | tr -s ' ' | cut -d ' ' -f 13 |sort |uniq
Now we know the role of tr command and we see different manner to use it. It is a powerful linux command when associating its different options because we can even sort with another command. You can refer tr command man pages for more information.