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 July 1, 2024 by Abhishek Sharma

The tr command in Unix/Linux is a powerful utility used for translating or deleting characters from standard input and writing the result to standard output. It is commonly employed for text processing tasks such as transforming case (e.g., converting lowercase to uppercase), removing specific characters, or replacing a set of characters with another set. The simplicity and efficiency of the tr command make it a valuable tool for shell scripting and command-line operations, providing users with a straightforward way to manipulate text streams.

What is tr Command in Unix Linux?

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 versatile and essential tool for text manipulation, offering a range of functionalities from character translation to deletion. Its ability to handle simple text transformations efficiently makes it a staple in the toolkit of Unix/Linux users, especially for those involved in shell scripting and command-line text processing. By understanding and leveraging the capabilities of the tr command, users can perform a wide array of text manipulations with ease and precision.

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:

1. What is the tr command used for in Unix/Linux?
Answer:
The tr command is used for translating, replacing, or deleting characters from standard input and writing the result to standard output. It is often used for tasks like converting case, deleting specific characters, or replacing characters.

2. How do you use the tr command to convert lowercase letters to uppercase?
Answer:
You can use the tr command with the [:lower:] and [:upper:] character classes to convert lowercase letters to uppercase. For example:

echo "hello world" | tr '[:lower:]' '[:upper:]'

This command will output "HELLO WORLD".

3. Can the tr command handle multiple character replacements simultaneously?
Answer:
Yes, the tr command can handle multiple character replacements simultaneously by specifying sets of characters. For example:

echo "abcde" | tr 'abc' '123'

This command will output "123de".

4. How do you delete specific characters using the tr command?
Answer:
To delete specific characters, use the -d option with tr. For example, to delete all digits from a string:

echo "hello123world" | tr -d '0-9'

This command will output "helloworld".

5. What does the -s option do in the tr command?
Answer:
The -s option in the tr command stands for "squeeze," and it replaces multiple consecutive occurrences of a character with a single occurrence. For example:

echo "hellooo   world" | tr -s ' o'

Leave a Reply

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