Linux tr Command with Examples

linux tr command 1

In Linux and Unix systems, tr is a command-line utility that translates, erases, and “squeezes” repeated characters – in fact, tr stands for “translate.” This guide explains how to use the tr command in Linux, with examples. It can be used for operations such as removing repeated characters, converting lowercase to uppercase, and basic replacing and removing of characters. It is often used in conjunction with other commands through piping.

Linux is the basic kernel/operating system core behind much of the technology we use on a day-to-day basis, including Android – the platform many of us will have on our mobile phones. It is open-source and easily configurable.

How to use the tr command

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

tr OPTION... SET1 [SET2]

The tr command accepts two sets of characters (usually the same length). It then replaces the characters from the first set with the corresponding characters from the second set. In this scenario, a SET is a string of characters and does include the special backslash-escaped characters.

Examples

Using a Range of Characters

You can also use ranges instead of character classes if you so please:

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

To convert the other way round, simply switch the places of the sets.

Convert case

Converting upper case to lower case or vice versa is one of the most common use cases of the tr command.

[:lower:] matches all lower case characters, and [:upper:] matches all uppercase characters.

echo 'Hello' | tr '[:lower:]' '[:upper:]'
HELLO

Remove blank lines

To delete blank lines, you need to “squeeze” the repetitive newline characters:

tr -s '\n' < input.txt > output.txt

In the above, we are using the redirection symbol, which then writes the command’s output to output.txt.

Remove all non-numeric characters

The following command deletes all non-numeric characters:

echo "The account number is 10879358" | tr -cd [:digit:]

[:digit:] stands for all digit characters. By using the -c option, the command removes characters that are non-numeric. The subsequent output will look like this:

10879358

Print $PATH directories on a new line

The $PATH environmental variable is a list of directories (colon delimited) that tells the shell which directories to look for executable files in when you type a command. To print each directory onto a separate new line, we need to match the colon (:) and then replace it with the new line command:

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

/usr/local/sbin
/usr/local/bin
/usr/sbin
/usr/bin
/sbin
/bin
/usr/games
/usr/local/games
/snap/bin

Put each word on a new line

To do this, we need to match all characters (that aren’t alpha-numeric) and then replace them with a new line:

echo 'Linux is the best operating system in the world' | tr -cs '[:alnum:]'

The output is:

Linux
is
the
best
operating
system
in
the
world

Replace characters

If we want to replace characters from the standard input (‘coding’) with the second set’s corresponding ones.

echo 'coding' | tr 'co' 'hi'

Each occurrence of “c” is replaced with “h”, and “o” with “i” with the result:

hiding

Character sets can also be defined using character ranges. For example, instead of writing:

echo 'coding' | tr 'abcd' 'wxyz'

you could use:

echo 'coding' | tr 'a-d' 'w-z'
yozing

When the -c option is used, tr replaces all the characters that are NOT in SET1.

In the following example, all characters except “li” will be replaced with the last character from the second set:

$ echo 'coding' | tr -c 'cod' 'xy'
liyyyiyyy

To echo a string without a new line, use the -n option.

The -d option tells tr to delete characters that are specified in SET1. When you want to do this without squeezing, you should specify only one set.

The command below will remove l, I, and z characters:

echo 'Linuxize' | tr -d 'liz'
SHARE:
nv-author-image

Stefan Durand

My name is Stefan, I'm the admin of LinuxScrew. I am a full-time Linux/Unix sysadmin, a hobby Python programmer, and a part-time blogger. I post useful guides, tips, and tutorials on common Linux and Programming issues. Feel free to reach out in the comment section.

Leave a Reply

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