Linux tr Command


Introduction

The tr (translate) command is a Linux utility that allows you to perform various transformations on text input. Whether you need to change case, remove repeating characters, remove characters, set complements or remove newline characters, tr can handle it all. In this article, we'll explore the various options available with the tr command and show you how to use them to transform text.

Syntax of tr Command

The basic syntax of the ‘tr’ command is as follows −

$ tr SET1 SET2 < inputfile

where −

  • SET1 is a set of characters to be replaced by the corresponding characters in SET2.

  • SET2 is a set of characters that replaces the corresponding characters in SET1.

  • input file is the input file to be processed, which can be omitted and replaced with input from STDIN (standard input).

Note that the characters in “SET1” and “SET2” can be specified in several ways, including individual characters, ranges of characters, and special characters for translation. The characters can also be specified in octal or hexadecimal notation.

For example, the tr command replaces uppercase characters in the input string "Hello World" with their lowercase equivalents. The character in SET1 is specified as ‘H’ and the character in SET2 is specified as ‘Y’.

$ echo "Hello World" | tr H Y
Yello world

Change Character Case with tr Command in Linux

tr can be used to change the case of input text in three different ways: by specifying the exact characters you want to convert, by specifying a range of characters for the conversion, and by specifying interpreted strings.

Specifying Exact Characters to tr Command

With this option, you can specify which characters of the input text you want to convert. The tr command replaces the characters of SET1 with the characters of SET2. For example −

$ echo "Hello World" | tr [A-Z] [a-z]
hello world

Another way to change the case of characters with tr is to specify a range of characters. This allows tr to change the case of any character within the specified range. The following example shows how to convert uppercase characters to lowercase −

$ echo "Hello World" | tr [A-Z] [a-z]
hello world

Specifying Character Ranges to tr Command

Match and convert characters by specifying interpreted sequences. For example, the lowercase character sequence is [:lower:] and the uppercase character sequence is [:upper:]. Specifying these two sequences tells tr to compare and convert the case of the character −

$ echo "HELLO WORLD" | tr [:upper:] [:lower:]
hello world

Remove Repeated Characters with tr Command in Linux

The ‘-s’ option to tr is useful for compressing repeated characters into a single instance. This option is especially useful when converting whitespace to tabs or newline characters and the input text contains multiple continuous whitespace. For example, the following input contains multiple whitespace −

$ echo "Hello 	World"
Hello 	World

Converting these whitespace to tabs roughly results in −

$ echo "Hello 	World" | tr ' ' '\t'
Hello   	 World

To avoid multiple tabs and whitespace in the output, you can use the -s option −

$ echo "Hello 	World" | tr -s ' ' '\t'
Hello    World

Delete characters with tr Command in Linux

Use the -d option to remove specific characters from input. In the following example, tr removes each instance of the character e −

$ echo "Hello World" | tr -d 'e'
Hllo World

You can also remove a group of characters by specifying interpreted strings. For example, remove all digits by specifying the sequence [:digit:]

$ echo "Hello 123 World" | tr -d [:digit:]
Hello  World

Complement Sets

Use the ‘-c’ option to complete the characters in SET1. This means that all characters not in the specified set will be removed. In the following example, we remove all characters except digits −

$ echo "Hello 123 World" | tr -c [:digit:]
123

Remove Diacritics with tr Command in Linux

Diacritical marks are symbols added to letters, such as accent marks, that change the sound of the letter. The sequence [=CHAR=] can be used to match all characters equivalent to the specified one. This can be useful for removing diacritics from characters.

For example, the following command uses tr to remove diacritics from characters −

$ echo "résumé" | tr -d '[=e=]'
rsum

Print Each Word Separately with tr Command in Linux

Sometimes it can be useful to print the contents of a file line by line or, in this case, word by word. The tr command can be used to accomplish this by using the -c option and replacing non-alphanumeric characters with a newline character.

Here's an example of how to print each word of a file separately −

$ cat file.txt | tr -c '[:alnum:]
' '[
*]'

Conclusion

In conclusion, the tr command is a versatile and powerful tool for transforming text on Linux. It allows you to perform various operations such as switching case, removing repeating characters, removing characters, complementing sets, removing newline characters, redirecting input, truncating sets, removing diacritics, printing each word separately and saving the output in a file. . This tutorial demonstrated these functions with practical examples to give you a clear understanding of how to use the tr command. Whether you are a novice or an advanced Linux user, this tool is definitely worth exploring for its many uses. Check out other Linux text utilities like sort command, pagerless terminal or best Linux text editors to further enhance your knowledge and experience.

Updated on: 19-Apr-2023

273 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements