Linux tr Command Usage and Example

The tr command is designed to translate, squeeze and/or delete characters in standard input (stdin)and write them to standard output (stdout). It is often used on the command line for string manipulation in a pipeline.

If you are unsure of what stdin and stdout are, we recommend you read our “Introduction to Linux IO, Standard Streams, and Redirection“.

In this article we will demonstrate the basic usage of the tr command with practical examples. We also take a VERY interesting and in-depth look at two oddly named options, complement and truncate.

Table of Contents

Using tr Command to Delete Characters

The most common usage for tr is to delete characters from an input stream. You can use the -d (–delete) option followed by the character, set of characters or an interpreted sequence.

Delete all instances of lower case “t”:

$ echo "Tongue tied and twisted just an earth bound misfit" | tr -d t
Tongue ied and wised jus an earh bound misfi

Delete all instances of the letter “t” lowercase or uppercase:

$ echo "Tongue tied and twisted just an earth bound misfit" | tr -d 'Tt'
ongue ied and wised jus an earh bound misfi

Delete all instances of letters in the range of “a-j”:

$ echo "Tongue tied and twisted just an earth bound misfit" | tr -d 'a-j'
Tonu t n twst ust n rt oun mst

You can also use interpreted sequences, for example, to delete all uppercase letters:

$ echo "Tongue tied and twisted just an earth bound misfit" | tr -d [:upper:]
ongue tied and twisted just an earth bound misfit

We will discuss interpreted sequences more later in the article.

Using tr Command for Substitution

The tr command can be used for character substitution. It takes two sets of characters as arguments and takes the characters in the first argument and replaces them with the characters in the second argument.

Replace all lower case “t” with a uppercase “T”:

$ echo "Tongue tied and twisted just an earth bound misfit" | tr 't' 'T'
Tongue Tied and TwisTed jusT an earTh bound misfiT

You can also use ranges here as well. As an extreme example, here we will replace letters a-d with the numbers 1-4. This effectively maps a to 1, b to 2, c to 3, and d to 4. Example:

$ echo "Tongue tied and twisted just an earth bound misfit" | tr 'a-d' '1-4'
Tongue tie4 1n4 twiste4 just 1n e1rth 2oun4 misfit

One of the most famous uses of the tr command is using it to substitute all lowercase for uppercase letters.

$ echo "Tongue tied and twisted just an earth bound misfit" | tr [:lower:] [:upper:]
TONGUE TIED AND TWISTED JUST AN EARTH BOUND MISFIT

Using tr Command to Remove Repeated Characters (Squeeze)

Another champion option of the tr command is -s (–squeeze-repeats). Squeeze means to take any number of repeated characters and replace then with a single instance of that character. This is often used to remove double spacing or excessive empty lines.

To demonstrate this I created the following test.txt file:

$ cat test.txt 
First Line with weird spacing.

Followed by a bunch of empty lines.



Then the last line with weird spacing.

We will use the tr command to squeeze any repeated spaces into a single space using the -s option.

$ cat test.txt | tr -s " "
First Line with weird spacing.
...OUTPUT TRUNCATED...

Now we will use the tr command to squeeze any repeated new lines.

$ cat test.txt | tr -s "\n"
First Line with weird spacing.
Followed by a bunch of empty lines.
Then the last line with weird spacing.

A little more straight forward example:

$ echo "putorius,,,,,,,,,,,,,,,net" | tr -s ,
putorius,net

As you can see, tr removed all the repeated commas and replaced them with a single comma.

You can also specify which character you want to replace it with. For example, it makes no sense to use a comma in a domain name. Let’s replace the long string of commas with a single period.

$ echo "putorius,,,,,,,,,,,,,,,,,,,,,net" | tr -s "," "."
putorius.net

Using tr Command Complement Option

I am not too proud to admit it took me a while to understand this option when I first came across it. The man page simply says “use the complement of SET1” which at first just confused me. But after I thought about it for a while and did some additional testing it started to make sense.

Leave a Reply

Your email address will not be published. Required fields are marked *