Tr Command in Linux with Examples

Introduction

tr is a Linux and Unix command-line application that translates, deletes, and squeezes characters from standard input and outputs the result to standard output.

The tr command can do things like remove repeated characters, convert uppercase to lowercase, and replace and remove basic characters. It's usually used in conjunction with other instructions via piping.

In this tutorial we'll show you how to use the tr command with practical examples and full explanations of the most frequent options. We will also address a few FAQs on tr Command in Linux.

How to Use the tr Command

Below is the syntax for the tr command:

tr OPTION... SET1 [SET2]

tr takes two sets of characters, usually of the same length, and replaces the first set's characters with the second set's comparable characters.

A SET is a sequence of characters that includes the special backslash-escaped characters.

By mapping the characters from the first set with the matching ones from the second set, tr will replace all characters from the standard input (vegastack) in the following example.

echo 'vegastack' | tr 'veg' 'red'

Each occurrence of the letter v is replaced with the letter r, the letter e with the letter e, and the letter g with the letter d:

Output

redastack

Character ranges can also be used to define character sets. For instance, rather than writing:

echo 'vegastack' | tr 'lmno' 'wxyz'

you can use:

echo 'vegastack' | tr 'l-n' 'w-z'

When the -c (--complement) option is used, tr substitutes all non-SET1 characters.

All characters except "ve" will be substituted with the final character from the second set in the example below:

echo 'vegastack' | tr -c 've' 'xy'
Output

veyyyyyyy

The output has one more visible character than the input, as you may have seen. Because the echo command prints an invisible newline character \n, which is also replaced with y, this is the case. Use the -n option to echo a string without a new line.

The -d (--delete) option instructs tr to remove characters from SET1. Only one set should be specified when eliminating characters without squeezing.

The following command will remove the v, e and c characters:

echo 'Vegastack' | tr -d 'vec'

Because the input contains an uppercase V and the e letter in the SET is lowercase, the V character is not removed.

Output

Vgastak

The -s (--squeeze-repeats) option substitutes the character set from the previous SET for a sequence of repeated occurrences.

tr removes the repeated space characters in the following example:

echo "GNU     \    Linux" | tr -s ' '
Output

GNU \ Linux

When SET2 is used, SET2 replaces the sequence of the character supplied in SET1.

echo "GNU     \    Linux" | tr -s ' ' '_'
Output

GNU_\_Linux

Before completing any additional processing, the -t (--truncate-set1) option tells tr to truncate SET1 to the length of SET2.

tr will reuse the last character of SET2 if SET1 is greater than SET2. Here's an illustration:

echo 'Vega stack' | tr 'abcde' '12'

The output reveals that SET1's character e is matched with SET2's most recent character, 2:

Output

Vega stac2

Use the following command with the -t option:

echo 'Vega stack' | tr -t 'abcde' '12'
Output

Vega stack

The last three characters of the SET1 have been erased, as can be seen. SET1 is replaced with 'ab,' which is the same length as SET2.

Combining options

You can also mix the options of the tr command. The following command, for example, substitutes all characters except I with 0 before squeezing the repeated 0 characters:

echo 'Vega stack' | tr -cs 'i' '0'
Output

0i0i0

Tr Command Examples

We'll go over a few instances of how to use the tr command in this section.

Convert lower case to upper case

One of the most common uses of the tr command is to convert lower case to upper case or vice versa. [:lower:] corresponds to all lowercase characters, while [:upper:] corresponds to all uppercase characters.

echo 'VegaStack' | tr '[:lower:]' '[:upper:]'
Output

VEGASTACK

You can also use ranges instead of character classes:

echo 'Vegastack' | tr 'a-z' 'A-Z'

Simply swap the sets' positions to change upper case to lower case.

Remove all non-numeric characters

All non-numeric characters are removed with the following command:

echo "my phone is 123-456-7890" | tr -cd [:digit:]

[:digit:] stands for all digit characters, while the -c option removes all non-digit characters from the command. This is what the final product will look like:

Output

1234567890

Put each word in a new line

We need to match any non-alphanumerical characters and replace them with a new line to put each word on a new line:

echo 'GNU is an operating system' | tr -cs '[:alnum:]' '\n'
Output

GNU
is
an
operating
system

Remove blank lines

Simply squeeze the repeating newline letters to remove the blank lines:

tr -s '\n' < file.txt > new_file.txt

The redirection symbol < is used in the command above to pass the content of file.txt to the tr command. The redirection > saves the command's output to new_file.txt.

When you type a command, the $PATH environmental variable is a colon-delimited list of directories that informs the shell where to look for executable files.

We need to match the colon (:) and replace it with the new line to print each directory on its own line:

echo $PATH | tr  ':' '\n'
Output

/usr/local/sbin
/usr/local/bin
/usr/sbin
/usr/bin
/sbin
/bin

FAQs on tr Command in Linux

How can I translate characters using the tr command? 

To translate characters using tr, you provide two sets of characters. The characters from SET1 will be replaced by the corresponding characters from SET2.

Can you provide an example of deleting characters with the tr command? 

Certainly! To delete specific characters using tr, you can omit the SET2 argument. For example: echo "Hello, World!" | tr -d 'l' will remove all occurrences of the letter 'l' from the input.

How does the tr command squeeze duplicate characters? 

The tr command can squeeze duplicate characters by specifying the -s option. It replaces a sequence of repeated characters with a single occurrence.

Can I use the tr command to convert characters to uppercase or lowercase? 

Yes, you can use tr to convert characters to either uppercase or lowercase. For example: echo "Hello, World!" | tr '[:lower:]' '[:upper:]' converts the input to uppercase.

Is the tr command case-sensitive? 

Yes, by default, the tr command is case-sensitive. It treats uppercase and lowercase characters as different sets.

Is there a limit to the number of characters I can specify in SET1 and SET2? 

No, there is no specified limit to the number of characters you can specify in SET1 and SET2. You can include as many characters as required.

Can the tr command be used to perform character encoding conversions? 

Yes, the tr command can be used to perform basic character encoding conversions by specifying the character sets appropriately in SET1 and SET2. However, for more complex encoding conversions, specialized tools like iconv are recommended.

Conclusion

tr is a command that allows you to translate or delete characters.

Despite its use, tr can only function with single characters. You should use sed or awk for more advanced pattern matching and text manipulation.

If you have any queries, please leave a comment below and we’ll be happy to respond to them.