10 tr Command Examples in Linux

[‘

n

tr (short for translate) is a useful command line utility that translates and/or deletes characters from stdin input, and writes to stdout. It is a useful program for manipulating text on the command line.

n

In this article, we will explain some useful tr command examples for Linux newbies.

n

The syntax for running tr command is as follows, where characters in SET1 are translated to characters in SET2.

n

$ tr flags [SET1] [SET2]rn

n

Linux tr Command Examples

n

1. A simple tr command use case is to change all lower case letters in text to upper case and vice versa, as shown below.

n

$ cat linux.txtrnrnlinux is my lifernlinux has changed my lifernlinux is best and everthing to me..:)rn

n

$ cat domains.txt | tr [:lower:] [:upper:]rnrnLINUX IS MY LIFErnLINUX HAS CHANGED MY LIFErnLINUX IS BEST AND EVERTHING TO ME..:)rn

n

2. Alternatively, you can use the following command to change all lower case letters to upper case in a file as shown.

n

$ cat linux.txt | tr [a-z] [A-Z]rnrnLINUX IS MY LIFErnLINUX HAS CHANGED MY LIFErnLINUX IS BEST AND EVERTHING TO ME..:)rn

n

3. To save the results written to stdout in a file for later processing, use the shell’s output redirection feature (>) as shown.

n

$ cat linux.txt | tr [a-z] [A-Z] >output.txtrn$ cat output.txt rnrnLINUX IS MY LIFErnLINUX HAS CHANGED MY LIFErnLINUX IS BEST AND EVERTHING TO ME..:)rn

n

4. In regards to the redirection, you can send input to tr using the input redirection and redirect the output to a file using the same command, as shown.

n

$ tr [a-z] [A-Z] < linux.txt >output.txtrn

n

5. Another useful feature is, you can use the -d flag to delete characters, for example to remove the spaces in the domain names using the following command.

n

$ cat domains.txtrnrnwww. tecmint. comrnwww. fossmint. comrnwww. linuxsay. comrn

n

$ cat domains.txt | tr -d '' rnrnwww.tecmint.comrnwww.fossmint.comrnwww.linuxsay.comrn

n

6. If there are repeated characters in a sequence (for instance double spaces) in the text you are processing, you can use the -s option to squeeze the characters leaving only one occurrence of it.

n

$ cat domains.txtrnrnwww.tecmint.....comrnwww.fossmint.comrnwww.linuxsay.comrn

n

$ cat domains.txt | tr -s '' rnrnwww.tecmint.comrnwww.fossmint.comrnwww.linuxsay.comrn

n

7. The -c option tells tr to use the complement in the given of SET. In this example, we want to delete all the letters and only leave the UID.

n

$ echo "My UID is $UID" | tr -cd "[:digit:]\n"rnORrn$ echo "My UID is $UID" | tr -d "a-zA-Z"rn

n

8. Here is an example of breaking a single line of words (sentence) into multiple lines, where each word appears in a separate line.

n

$ echo "My UID is $UID"rnrnMy UID is 1000rnrn$ echo "My UID is $UID" | tr " "  "\n"rnrnMy rnUID rnis rn1000rn

n

9. Related to the previous example, you can also translate multiple lines of words into a single sentence as shown.

n

$ cat uid.txtrnrnMy rnUID rnis rn1000rnrn$ tr "\n" " " < uid.txtrnrnMy UID is 1000rn

n

10. It is also possible to translate just a single character, for instance a space into a “ : ” character, as follows.

n

$ echo "Tecmint.com =>Linux-HowTos,Guides,Tutorials" | tr " " ":"rnrnTecmint.com:=>Linux-HowTos,Guides,Tutorialsrn

n

There are several sequence characters you can use with tr, for more information, see the tr man page.

n

$ man trrn

n

That’s all! tr is a useful command for manipulating text on the command line. In this guide, we showed some useful tr command usage examples for Linux newbies. You can share your thoughts with us via the comment form below.

n

‘]