Linux Tactic

Transform Your Text with the Versatile Linux ‘tr’ Command

Have you ever come across a situation where you need to translate one set of characters to another or delete specific characters from a text file in Linux? This is where the ‘tr’ command comes into play.

In this article, we will explore what tr command is, how it can be used, and practical examples to help understand it better.

1) Overview of tr command

The ‘tr’ command in Linux stands for ‘translation’. It is a command-line utility that is used to translate or substitute a set of characters in a text file with another set of characters.

This command is mostly used to replace or delete certain characters in a text file. One of the primary features of the tr command is its flexibility.

It can be used to replace characters, delete characters, or even perform simple encryption and decryption tasks. The tr command can also be combined with other commands, such as awk and sed, to form powerful and complex text editing commands.

2) Comparison of tr command with awk and sed commands

‘Tr’ command, ‘awk’ command, and ‘sed’ command are three commonly used text manipulation utilities in Linux. While they all share similar goals, they differ in functionality and usage.

The ‘awk’ command is a powerful tool that can be used to manipulate columns in a text file using pattern matching. It is particularly useful for dealing with structured data.

The ‘sed’ command is mostly used to search and replace characters or strings in a text file. On the other hand, the ‘tr’ command is used to translate or substitute one set of characters with another.

It is particularly useful for tasks such as converting lowercase text to uppercase, removing specific characters or multiple spaces, or even truncation of a search pattern.

3) Practical examples of tr command in Linux

a) Syntax of tr command and its options

The syntax of the ‘tr’ command is straightforward. The basic syntax is:

tr [options] set1 [set2]

The ‘set1’ specifies the source character set, while ‘set2’ specifies the destination character set.

Let’s go through some practical examples to understand better.

b) Conversion of lower case to upper case and vice versa

The ‘tr’ command can be used to convert lowercase text to uppercase and vice versa. To change lowercase text to uppercase, we can use:

$ tr ‘[:lower:]’ ‘[:upper:]’ < input_file.txt > output_file.txt

Similarly, to convert uppercase text to lowercase, we can use:

$ tr ‘[:upper:]’ ‘[:lower:]’ < input_file.txt > output_file.txt

c) Replacement of one set of characters with another

The ‘tr’ command can be used to replace one set of characters with another. For instance, to replace all ‘e’ characters with ‘a’ in a text file, we can use:

$ tr ‘e’ ‘a’ < input_file.txt > output_file.txt

We can also replace specific characters with another set of characters.

For instance, if we want to replace all digits with an asterisk (‘*’), we can use:

$ tr ‘[:digit:]’ ‘*’ < input_file.txt > output_file.txt

d) Deletion of specific character(s)

The ‘tr’ command can also be used to delete specific characters from a text file. For instance, to delete all spaces from a text file, we can use:

$ tr -d ‘ ‘ < input_file.txt > output_file.txt

Similarly, to delete all vowels from a text file, we can use:

$ tr -d ‘aeiou’ < input_file.txt > output_file.txt

e) Removal of repetitive characters like multiple spaces

The ‘tr’ command can also be used to remove repetitive characters such as multiple spaces. For instance, to remove all extra spaces in a text file and replace them with a single space, we can use:

$ tr -s ‘ ‘ < input_file.txt > output_file.txt

f) Removal of all the non-digit characters

The ‘tr’ command can also be used to remove all non-digit characters from a text file. For instance, to remove all non-digit characters from a text file, we can use:

$ tr -cd ‘[:digit:]’ < input_file.txt > output_file.txt

g) Truncation of a search pattern

The ‘tr’ command can also be used to truncate a search pattern in a text file. For instance, if we want to truncate all text after the first occurrence of ‘the’, we can use the following command:

$ tr -s ‘ ‘ ‘n’ < input_file.txt | sed -n '/^the/,$p' | tr 'n' ' ' > output_file.txt

Conclusion

In conclusion, the ‘tr’ command is a powerful and flexible utility for performing basic text editing tasks in Linux. It can be used to convert lowercase text to uppercase and vice versa, replace one set of characters with another, delete specific characters, remove repetitive characters, remove all non-digit characters, and truncate a search pattern.

By combining the ‘tr’ command with other tools like awk and sed, we can create more complex text editing commands to suit our specific needs. In this article, we explored the tr command in Linux and how it is used for basic text editing tasks such as character conversion, replacement, deletion, and truncation.

We compared it with other text manipulation utilities and discussed the syntax and various options of the tr command. The article emphasized the importance of tr command’s flexibility and its usefulness when combined with other tools like awk and sed to create complex text editing commands.

The takeaway is that, although the tr command is basic, it is a powerful utility that every Linux user should be familiar with to make their text editing tasks more efficient.

Popular Posts