≡ Menu

8 Linux TR Command Examples

tr is an UNIX utility for translating, or deleting, or squeezing repeated characters. It will read from STDIN and write to STDOUT.

tr stands for translate.

Syntax

The syntax of tr command is:

$ tr [OPTION] SET1 [SET2]

Translation

If both the SET1 and SET2 are specified and ‘-d’ OPTION is not specified, then tr command will replace each characters in SET1 with each character in same position in SET2.

1. Convert lower case to upper case

The following tr command is used to convert the lower case to upper case

$ tr abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ
thegeekstuff
THEGEEKSTUFF

The following command will also convert lower case to upper case

$ tr [:lower:] [:upper:]
thegeekstuff
THEGEEKSTUFF

You can also use ranges in tr. The following command uses ranges to convert lower to upper case.

$ tr a-z A-Z
thegeekstuff
THEGEEKSTUFF

2. Translate braces into parenthesis

You can also translate from and to a file. In this example we will translate braces in a file with parenthesis.

$ tr '{}' '()' < inputfile > outputfile

The above command will read each character from “inputfile”, translate if it is a brace, and write the output in “outputfile”.

3. Translate white-space to tabs

The following command will translate all the white-space to tabs

$ echo "This is for testing" | tr [:space:] '\t'
This	is	for	testing

4. Squeeze repetition of characters using -s

In Example 3, we see how to translate space with tabs. But if there are two are more spaces present continuously, then the previous command will translate each spaces to a tab as follows.

$ echo "This   is   for testing" | tr [:space:] '\t'
This			is			for	testing

We can use -s option to squeeze the repetition of characters.

$ echo "This   is   for testing" | tr -s [:space:] '\t'
This	is	for	testing

Similarly you can convert multiple continuous spaces with a single space

$ echo "This  is  for testing" | tr -s [:space:] ' '
This is for testing

5. Delete specified characters using -d option

tr can also be used to remove particular characters using -d option.

$ echo "the geek stuff" | tr -d 't'
he geek suff

To remove all the digits from the string, use

$ echo "my username is 432234" | tr -d [:digit:]
my username is

Also, if you like to delete lines from file, you can use sed d command.

6. Complement the sets using -c option

You can complement the SET1 using -c option. For example, to remove all characters except digits, you can use the following.

$ echo "my username is 432234" | tr -cd [:digit:]
432234

7. Remove all non-printable character from a file

The following command can be used to remove all non-printable characters from a file.

$ tr -cd [:print:] < file.txt

8. Join all the lines in a file into a single line

The below command will translate all newlines into spaces and make the result as a single line.

$ tr -s '\n' ' ' < file.txt
Add your comment

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

Comments on this entry are closed.

  • mre December 19, 2012, 10:42 am

    Hello

    How can I order in script lower words to upper the first letter of word only ?

    ex: ramesh natarajan > Ramesh Natarajan

    Thank you!

  • Jalal Hajigholamali December 19, 2012, 8:45 pm

    Hi,
    Thanks for nice article.

    Hi “mre”

    tr can not solve your problem…

  • Goxxi December 20, 2012, 2:18 am

    Hi,

    I think, that [:lower:], [:digit:], …. must be quoted
    to protect this symbolic names against filename expansion!

    Command line example:

    $ touch d i g t
    $ tr [:digit:] ‘*’
    tr: extra operand `i’
    Try `tr –help’ for more information.
    $

    With regards,
    Goxxi

  • Kamal Kishore December 24, 2012, 6:37 am

    8) Join all the lines in a file into a single line
    The below command will translate all newlines into spaces and make the result as a single line.

    $ tr -s ‘\n’ ‘ ‘ < file.txt

    also can use tr '\n' <file.txt # here we get the same result without using -s (switch)

  • Saravanan February 6, 2013, 12:55 pm

    Nice article.

  • VIVEK August 1, 2013, 8:19 pm

    echo “This is for testing” | tr [:space:] ‘\t’ …
    This command works only wen I enclose single quotes in between ‘[:space:]’..
    i.e echo “This is for testing” | tr ‘[:space:]’ ‘\t’

  • surendra December 23, 2013, 5:09 am

    echo “welcome”|tee word|cut -c1|tr [a-z] [A-Z]|tee word1|echo “`cat` `cat word`”|cut -c1,4-10.
    this will work to make the first letter of word to be capital

  • linux torvalds February 16, 2014, 11:37 am

    how to replace multiple characters with only one given characters ?

  • Bhupender Singh March 31, 2014, 7:15 pm

    This is how you can change multiple characters to only one character

    echo `tr wabd A filename to save the output

  • linux torvalds April 9, 2014, 7:13 am

    @bhupender singh
    hey,
    I don think ‘wabd’ are options for tr; check by `man tr` command…

  • linux torvalds April 9, 2014, 7:19 am

    @bhupender singh
    hey,
    I don think ‘wabd’ are options for tr; check by `man tr` command…

  • maria May 14, 2014, 7:18 am

    how to delete all control symbols from a line except NULL and new line?
    I try
    tr -d [[:cntrl:]]
    but i need to save \n

  • Anonymous November 9, 2014, 1:48 pm

    fast commend is not working
    fast commmend ANS -tr -s ” [a-z]” “[A-Z” < file name

  • Vijay January 8, 2015, 5:49 pm

    Dear Lakshmanan,

    Your tr of lower to upper is incorrect. You need to surround the brackets in single quotes.

    Sincerely,
    Vijay

  • Bensen June 5, 2015, 2:25 am

    Hi! Thanks for the article.

    I am trying to to use your command from nr 2 to change all text within a file from lower to upper. however bash just deletes everything!

    tr [:lower:] [:upper:] text.txt

    any advice on how i could avoid this? i want to overwrite the same file basically input and output at the same location. thx!

  • Pratyush July 20, 2015, 5:49 am

    @Bensen
    If this is your exact command
    tr [:lower:] [:upper:] text.txt
    Then it needs to be changed to
    tr [:lower:] [:upper:] < text.txt

    You need to redirect the file content, using '<' , to the 'tr' command

  • Maswood Alam November 13, 2015, 2:23 am

    [root@abc alam]# cat kbc |tr [:lower:] [:upper:]
    HI ALAM HOW ARE YOU []
    I [AM FINE] BRO
    [root@nldtlx07 alam]#
    How can I also translate the braces in same command
    cat kbc |tr [:lower:] [:upper:] && [] {}====> Its not working

  • Dinis November 24, 2015, 5:46 am

    How can I change a text file with only NL line separators to a DOS one?

  • Ivan February 11, 2016, 10:39 am

    Hi, i working on an assignment where i have to encrypt a text file.
    I finish most of the sub part but i could not solve the main part.

    Im suppose to add a “key” into a-z. for example, the key is ZEROZEBRA->ZEROBA
    ( i also have to remove the alphabet if they are the same)

    plaintext alphabet ->abcdefghijklmnopqrstuvwxyz
    ciphertext alphabet->zerobacdfghijklmnpqstuvwxy

    notice that key is place to the left and the order of the alphabet is changed

    After that, i use the ciphertext to encrypt a text file( i done this part).

  • JP March 19, 2016, 11:14 am

    How do you combine many [SET1](s) and [SET2](s) into one tr -d command? I want to see an example of this. For example, I want to remove both the ’00’ and ‘E-‘ in one tr -d command from a file1.

  • HTechnology March 26, 2016, 4:32 am

    Thanks, it helped me, I like the organisation of this tuto, it realy saves a time then reading the documentation topic of command 🙂

  • arun April 8, 2016, 3:09 am

    how to replace a character with multiple table.For example I need to replace a by 4 tabs

  • Anonymous March 23, 2017, 7:31 am

    what happens in converting lower case to upper case letter if the 2nd string had more characters than 1st character

  • Pothi Kalimuthu April 17, 2017, 4:45 am

    Thanks for the hints. I had to remove colon from a “MAC” address (of an ethernet card). The following worked…

    echo $MACADDRESS | tr -d ‘:’

    Of course, I needed to replace “MACADDRESS” with the actual MAC address.

  • David May 18, 2017, 9:44 am

    I’d add a simple, but useful #9 example:

    tr ‘\0’ ‘\n’ < /proc//environ

    to inspect the environment of a running process.
    The ‘environ’ files use the C convention of terminating strings with a NULL character, which makes them difficult to process e.g. with grep.