tr Command in Linux with Examples

This tutorial explains Linux “tr” command, options and its usage with examples.

tr – translate

Description :

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

Usage :

tr [-Ccsu] string1 string2

In this form, the characters in the string string1 are translated into the characters in string2 where the first character in string1 is translated into the first character in string2 and so on. If string1 is longer than string2, the last character found in string2 is duplicated until string1 is exhausted.

advertisement
advertisement

tr [-Ccu] -d string1

In this form, the characters in string1 are deleted from the input.

tr [-Ccu] -s string1

In this form, the characters in string1 are compressed as described for the -s option (see below).

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

tr [-Ccu] -ds string1 string2

In the fourth form, the characters in string1 are deleted from the input, and the characters in string2 are compressed as described for the -s option.

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

Options :

-C
Complement the set of characters in string1, that is “-C ab” includes every character except for ‘a’ and ‘b’.
-c
Same as -C but complement the set of values in string1.
-d
Delete characters in string1 from the input.
-s
Squeeze multiple occurrences of the characters listed in the last operand (either string1 or string2) in the input into a single instance of the character. This occurs after all deletion and translation is completed.
-u
Guarantee that any output is unbuffered.

advertisement

Note :

Strings are specified as sets of characters. Most represent themselves. Interpreted sequences are:

\NNN
character with octal value NNN (1 to 3 octal digits)
\\
backslash
\a
audible BEL
\b
backspace
\f
form feed
\n
new line
\r
return
\t
horizontal tab
\v
vertical tab
CHAR1-CHAR2
all characters from CHAR1 to CHAR2 in ascending order
[CHAR*] in SET2, copies of CHAR until length of SET1
[CHAR*REPEAT] REPEAT copies of CHAR, REPEAT octal if starting with 0
[:alnum:] all letters and digits
[:alpha:] all letters
[:blank:] all horizontal whitespace
[:cntrl:] all control characters
[:digit:] all digits
[:graph:] all printable characters, not including space
[:lower:] all lower case letters
[:print:] all printable characters, including space
[:punct:] all punctuation characters
[:space:] all horizontal or vertical whitespace
[:upper:] all upper case letters
[:xdigit:] all hexadecimal digits
[=CHAR=] all characters which are equivalent to CHAR

Examples :

1. Convert lower case to upper case

advertisement
$ tr abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ
sanfoundry
SANFOUNDRY

The following command will also convert lower case to upper case

$ tr [:lower:] [:upper:]
sanfoundry
SANFOUNDRY

OR

$ tr a-z A-Z
sanfoundry
SANFOUNDRY

2. Translate white-space to tabs

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

3. 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”.

4. Squeeze repetition of characters using -s

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

5. Delete specified characters using -d option

To remove all the digits from the string, use

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

6. 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

7. 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

8. Complement the sets using -c option

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

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

Sanfoundry Global Education & Learning Series – 1000 Linux Tutorials.

If you wish to look at all Linux commands and their usage examples, go to Linux Commands Tutorial.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.