Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

tr Command in Unix Linux with Examples

Last Updated on November 21, 2023 by Abhishek Sharma


The tr command in Unix/Linux is a versatile and powerful utility that stands for "translate" or "transliterate." It is used to replace or delete characters in a given input stream (a file or data from a command) and produce the modified output. The tr command is particularly useful for transforming or cleaning up text.

What is tr Command in Unix Linuz?

In the Unix/Linux command-line environment, the tr command is a versatile tool for translating or deleting characters. Its primary purpose is to transform the contents of a file or standard input by replacing certain characters with others or by deleting specific characters. In this article, we will explore the functionality of the tr command and provide practical examples of its usage.

Basic Syntax of tr Command in Unix Linux:

The basic syntax of the tr command is as follows:

tr [options] SET1 [SET2]
  • SET1: Specifies the set of characters to be replaced or deleted.
  • SET2: Specifies the replacement set of characters.

Examples of tr Command in Unix Linux
Below are some examples of tr Command in Unix Linux:

Example 1: Basic Character Replacement

echo "Hello, World!" | tr 'l' 'L'

This command replaces all occurrences of the lowercase letter ‘l’ with the uppercase letter ‘L’. The output will be: "HeLLo, WorLd!"

Example 2: Deleting Characters

echo "Remove vowels" | tr -d 'aeiou'

The -d option deletes characters from the input set. In this example, it removes all vowels from the given string, resulting in "Rmv vwls."

Example 3: Squeezing Repeated Characters

echo "too many    spaces" | tr -s ' '

The -s option squeezes repeated characters into a single character. In this case, it compresses consecutive spaces into one space, producing "too many spaces."

Example 4: Translating Character Ranges

echo "12345" | tr '0-9' 'a-j'

This command translates each digit to its corresponding lowercase letter, resulting in "abcdefghij."

Example 5: Translate with Complement Set

echo "Hello, World!" | tr -c 'aeiou' '*'

The -c option complements the specified character set. In this example, it replaces all non-vowel characters with an asterisk, resulting in "eo, old!"

Example 6: Uppercase to Lowercase

echo "Convert TO lowercase" | tr 'A-Z' 'a-z'

This command converts all uppercase letters to lowercase, producing "convert to lowercase."

Conclusion:
The tr command in Unix/Linux is a powerful and flexible tool for character translation and manipulation. Whether you need to replace characters, delete specific characters, or perform more complex transformations, the tr command is an essential component of text processing in the command line. Experimenting with different options and character sets will allow you to tailor the tr command to meet your specific text transformation needs.

FAQs Related to tr Command in Unix Linux with Examples

Here are some of the FAQs related to tr Command in Unix Linux with Example:

Q1: What is the purpose of the tr command?
A: The tr command in Unix/Linux is used for character translation and manipulation. It can replace or delete specific characters, squeeze repeated characters, and perform various other transformations on text input.

Q2: How is the tr command different from sed and awk?
A: While tr is specifically designed for character translation and deletion, sed (stream editor) and awk (text processing tool) are more versatile and can perform more complex text manipulations, including pattern matching and substitution.

Q3: Can the tr command be used on files?
A: Yes, the tr command can be used on files. You can either provide a file as an argument or use input redirection (<) to read from a file. For example, tr 'a-z' 'A-Z' < input.txt.

Q4: How can I replace multiple characters using the tr command?
A: To replace multiple characters, you can specify multiple characters in the SET1 and SET2 arguments. For example, tr 'abc' '123' will replace 'a' with '1', 'b' with '2', and 'c' with '3'.

Q5: Can the tr command handle Unicode characters?
A: The tr command is primarily designed for single-byte character sets. While it can be used with Unicode characters, it may not handle multibyte characters correctly, and tools like iconv are more suitable for Unicode conversions.

Q6: How does the -s option work in the tr command?
A: The -s option squeezes repeated characters into a single character. It compresses consecutive occurrences of the characters specified in SET1 to a single character in the output.

Leave a Reply

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