How to use the Linux tr command through examples

The tr command is used to translate characters in Linux. It requires two sets of characters. It replaces the characters that appear in the first group with the corresponding characters in the second group. For example, the tr command can be used to change lowercase letters to uppercase letters.

If the strings are the same length, the occurrence of each character in set1 is replaced by the corresponding character in set2. If set1 is longer than set2, the tr command assigns the last character of set2 to each unspecified position after the end of the specified string.

table of Contents

  • tr command and its syntax
  • 1) Use the tr command to change the case
  • 2) Delete the characters that appear and delete the NULL characters
  • 3) Replace unmatched characters with another set of characters
  • 4) Replace the sequence of the same character
  • 5) Convert each character sequence to a single newline
  • 6) Create a list of unique words contained in the file, one per line
  • 7) Sort the results obtained using the tr command alphabetically

tr command and its syntax

First, let’s look at the syntax of the 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 the tr command to change the case

Translate the word "welcome" to uppercase:

# echo "welcome to linoxide" | tr [:lower:] [:upper:] 
WELCOME TO LINOXIDE

Go to a file and print the result to the console's standard output only. Our files are 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?

Enter a file and print the results to another file to save it. Our original files are unaffected, and we save the results.

# tr "a-z" "A-Z" < file > output

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

2) Delete the characters that appear and delete the NULL characters

-d Option deletes all occurrences of the specified character:

# 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 remove all NULL characters in the file, you can execute

tr -d '' < file > output

3) Replace unmatched characters with another set of characters

-c option is used to replace unmatched characters with another set of characters. For ASCII characters ranging from 01 to 0377 octal, set1 is supplemented.

# echo "Abc123d56E" | tr -c 'A' 't'
Atttttttttt

4) Replace the sequence of the same character

-s The option converts each sequence of repeated newlines to a single newline, which replaces 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) Convert each character sequence to a single newline

Converts each sequence of characters except lowercase and uppercase letters to a single newline character. * (Asterisk) causes TR The command repeats the newline character a sufficient number of times so that the second string is as long as the first.

# cat file | tr -cs "[:alpha:]" "n"
Can
you
see
how
tr
command
can
be
wonderful

6) Create a list of 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 the results obtained using the tr command alphabetically

The following command will list all 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 what the tr command does and we see different ways to use it. It is a powerful linux command when different options are associated, because we can even use another command for sorting. You can refer to the tr command man page for more information.

Also read:

  • 8 Linux Tee command example
  • 106 Linux Commands-Brief Overview with Examples
Sidebar