Skip to main content

tr - Transliterate or Delete Characters

The tr command in Linux, short for translate or transliterate, is a useful utility for transforming or deleting characters from standard input and writing the result to standard output. It is commonly used for tasks such as changing lowercase to uppercase, squeezing repeating characters, deleting specific characters, and more.

Syntax of tr

The basic syntax of the tr command is as follows:

tr [OPTIONS] SET1 [SET2]

Here SET1 is the set of characters to be replaced or removed, and SET2 is the set of characters to replace with.

Options for tr

The tr command does not have shorthand for its options as other commands might. Instead, it uses full words for its options. Here are some commonly used options:

OptionDescription
-dDelete characters in SET1; ignores SET2.
-sSqueeze repeated characters in SET1 into a single character.
-cUse the complement of SET1.
-tTruncate SET1 to the length of SET2.

Examples of tr

Let's create a file named sample.txt using vim:

vim sample.txt

Press i to switch to insert mode and enter the following text:

Welcome to the world of Linux.
Where you learn about commands.
This is a sample text file.

Press Esc, then type :wq and press Enter to save and exit vim.

Certainly! Here's a detailed explanation of each example provided for using the tr command:

Example 1: Convert Lowercase to Uppercase

Command:

tr 'a-z' 'A-Z' < sample.txt

Explanation: This command translates all lowercase letters to uppercase letters in the file sample.txt. Here's how it works:

  • 'a-z' specifies the range of characters to be translated, which in this case is all lowercase letters.
  • 'A-Z' specifies the range of characters to replace the original set, which is all uppercase letters.
  • The < symbol is used to redirect the content of sample.txt to the standard input of the tr command.
  • tr reads the input, and for every lowercase letter, it finds in the range 'a-z', it replaces it with the corresponding uppercase letter from the range 'A-Z'.
  • The transformed text is written to standard output (which can be seen in the terminal or redirected to a file).

Example 2: Delete Characters

Command:

tr -d 'aeiou' < sample.txt

Explanation: This command deletes all occurrences of the specified characters in sample.txt. Here's the breakdown:

  • The -d option tells tr to delete characters.
  • 'aeiou' is the set of characters tr will look for in the input to delete.
  • Using < sample.txt again redirects the content of the file to tr.
  • As tr processes the input, it removes all lowercase vowels, as specified in the set.
  • The resulting text, with the specified characters deleted, is written to standard output.

Example 3: Squeeze Repeated Characters

Command:

tr -s '\n' < sample.txt

Explanation: This command squeezes, or reduces, multiple sequential newline characters into a single newline in sample.txt.

  • The -s option tells tr to squeeze repeated characters.
  • '\n' specifies the newline character, which tr will squeeze.
  • The content of sample.txt is redirected to tr.
  • If tr encounters repeated newline characters, it will replace them with a single newline character.
  • The processed text, with extra newlines squeezed out, is sent to standard output.

Example 4: Complement Set

Command:

tr -cd 'a-zA-Z\n' < sample.txt

Explanation: This command deletes all characters that are NOT part of the specified set from sample.txt.

  • The -c option is used to refer to the complement of the set provided.
  • The -d option tells tr to delete characters.
  • 'a-zA-Z\n' is the set of characters that will NOT be deleted (alphabetic characters and newline).
  • As before, sample.txt is redirected into tr.
  • tr processes the text and deletes any character that is not an alphabetic character or a newline.
  • The output is the filtered text that only contains the characters in the set, written to standard output.

Example 5: Translate Characters

Command:

tr '0-9' '9876543210' < sample.txt

Explanation: Assuming sample.txt contains numbers, this command will map each digit to its "inverse" based on the provided sets.

  • '0-9' is the set of characters representing all digits which will be translated.
  • '9876543210' is the set of characters that will replace the original set, effectively inverting the digits.
  • The file sample.txt is redirected to tr.
  • tr reads each character and if it's a digit, it replaces it with the corresponding digit from the second set.
  • The result, with all digits inverted, is displayed in the standard output.

Combining Options

Command:

tr -ds 'aeiou' '\n' < sample.txt

Explanation: This command deletes all lowercase vowels and also squeezes newlines in sample.txt.

  • The -d option signals the deletion of the specified characters in the first set, 'aeiou'.
  • The -s option, along with '\n', tells tr to squeeze consecutive newline characters down to a single newline.
  • The redirection < sample.txt feeds the file content to tr.
  • As tr processes the text, it simultaneously deletes the vowels and squeezes the newlines.
  • The resulting text, without lowercase vowels and with consecutive newlines reduced to single ones, is produced on standard output.

These examples demonstrate how tr can be a powerful tool for modifying and manipulating text directly from the command line or within shell scripts.

What Can You Do Next 🙏😊

If you liked the article, consider subscribing to Cloudaffle, my YouTube Channel, where I keep posting in-depth tutorials and all edutainment stuff for software developers.

YouTube @cloudaffle