10 tr Command Examples in Linux

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.

This article will explain some useful tr command examples for Linux newbies.

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

$ tr flags [SET1] [SET2]

Linux tr Command Examples

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

$ cat linux.txt

linux is my life
linux has changed my life
linux is best and everthing to me..:)
$ cat linux.txt | tr [:lower:] [:upper:]

LINUX IS MY LIFE
LINUX HAS CHANGED MY LIFE
LINUX IS BEST AND EVERTHING TO ME..:)

2. Alternatively, you can use the following command to change all lowercase letters to uppercase in a file as shown.

$ cat linux.txt | tr [a-z] [A-Z]

LINUX IS MY LIFE
LINUX HAS CHANGED MY LIFE
LINUX IS BEST AND EVERTHING TO ME..:)

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

$ cat linux.txt | tr [a-z] [A-Z] >output.txt
$ cat output.txt 

LINUX IS MY LIFE
LINUX HAS CHANGED MY LIFE
LINUX IS BEST AND EVERTHING TO ME..:)

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.

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

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.

$ cat domains.txt

www. tecmint. com
www. fossmint. com
www. linuxsay. com
$ cat domains.txt | tr -d '' 

www.tecmint.com
www.fossmint.com
www.linuxsay.com

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.

$ cat domains.txt

www.tecmint.....com
www.fossmint.com
www.linuxsay.com
$ cat domains.txt | tr -s '' 

www.tecmint.com
www.fossmint.com
www.linuxsay.com

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.

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

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

$ echo "My UID is $UID"

My UID is 1000

$ echo "My UID is $UID" | tr " "  "\n"

My 
UID 
is 
1000

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

$ cat uid.txt

My 
UID 
is 
1000

$ tr "\n" " " < uid.txt

My UID is 1000

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

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

Tecmint.com:=>Linux-HowTos,Guides,Tutorials

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

$ man tr

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.

Aaron Kili
Aaron Kili is a Linux and F.O.S.S enthusiast, an upcoming Linux SysAdmin, web developer, and currently a content creator for TecMint who loves working with computers and strongly believes in sharing knowledge.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

19 thoughts on “10 tr Command Examples in Linux”

  1. Just to inform there is a typo in the 6th point. Have a look:

    $ cat domains.txt | tr -s ” <= Original
    $ cat domains.txt | tr -s '.' <= Edited: Notice the "." (dot)

    Output:

    www.tecmint.com
    www.fossmint.com
    www.linuxsay.com

    Reply
  2. Thanks for the great examples. There is a small typo in the first example.

    You do

    $ cat linux.txt

    linux is my life
    linux has changed my life
    linux is best and everthing to me..:)

    and then

    $ cat domains.txt | tr [:lower:] [:upper:]

    LINUX IS MY LIFE
    LINUX HAS CHANGED MY LIFE
    LINUX IS BEST AND EVERTHING TO ME..:)

    This should be instead.

    $ cat linux.txt | tr [:lower:] [:upper:]

    Reply
  3. I have a string that I assigned to a key combination, ctrl+k. The intention is to take the selected (highlighted) text in a textbox in one application, strip out all dots, replace all spaces with underscores, then place the amended text in the clipboard. the line is:

    xcopy -o | tr " " "_" | tr -d "." | xcopy -selection clipboard
    

    When I test the line in a terminal, it works fine. If I try it using ctrl+k, it leaves dots in the output string in the clipboard. This is puzzling me.

    Reply
  4. Hi Gurus,

    I tried to add a delimiter into fix log to get something like:

    From

    20111107-10:52:25.272: 8=FIX.4.49=7735=A52=20111107-10:52:25.9263098=010=176
    

    to this

    20111107-10:52:25.272: 8=FIX.4.4|9=77|35=A|52=20111107-10:52:25.926|108=3098=010=176
    

    this my command

    $ grep 38=100 FIXLOG.log|tr " " "|"
    

    But doesn’t work

    Reply
  5. You forgot the most important use for tr which is to implement a rot13 encoder-decoder; that is translate between the range a-m and n-z.

    # tr a-z n-za-m
    # tr a-zA-Z n-za-mN-ZA-M
    
    Reply
  6. -s command is incorrect in your post. You currently have -d showing in the example. Other than the syntax error and that one example it’s a pretty good introduction to the TR command.

    Reply

Got something to say? Join the discussion.

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.