Explore the Linux tr Command with 10 Examples

Try this guide with our instant dedicated server for as low as 40 Euros

linux tr command

The tr utility in Linux is a versatile option for modifying text from standard input or files.

You can use it to change the way text is displayed in the terminal. For instance, tr can convert uppercase to lowercase and vice versa, delete or transform a set of characters, and handle newline characters.

In this comprehensive tutorial, we will take a close look at the tr utility in Linux and see how you can apply it to solve the 10 common formatting problems.

Table of Contents

  1. A Brief Introduction to the tr Utility
  2. The Linux tr Command Syntax
    1. The Idea of SET in tr
    2. The Linux tr Command Options
  3. 10 Linux tr Examples to Manipulate Text
    1. Prerequisites
    2. Example #1: Change Character Case
    3. Example #2: Remove Repeated Characters
    4. Example #3: Delete Characters
    5. Example #4: Complement Sets
    6. Example #5: Remove Newline Characters
    7. Example #6: Redirect Into tr
    8. Example #7: Truncate Set Length
    9. Example #8: Remove Diacritics
    10. Example #9: Print Each Word Separately
    11. Example #10: Save Output to File
  4. Conclusion
  5. FAQ

A Brief Introduction to the tr Utility

You may ask about the benefits of using tr when you have custom-built text processing utilities like sed and awk.

A simple answer to this question is that the tr command in Linux is a simple text manipulation utility used for translating or deleting characters. Its primary function is to replace or remove specific characters in a text stream (stdin or files).

More importantly, tr is part of the core utilities in Unix-like operating systems, including Linux. As such you can be sure that tr is installed and available on all popular Linux distributions.

It is important to note that the tr utility can’t read files directly from the input. That’s why it’s often paired with the cat command, where the output is piped into the tr utility for advanced text processing.

The Linux tr Command Syntax

The basic syntax for the tr command in Linux is as follows:

# tr [OPTIONS] SET1 [SET2]

Executing tr without additional parameters substitutes each character in SET1 with its corresponding character in SET2, based on their respective positions.

Consider the following command that substitutes characters in SET1 (e) with SET2 (o):

# echo “Hello” | tr e o

# echo “Hello” tr e o

The Idea of SET in tr

The tr utility works with characters. As such, it uses SETs as a collection of characters that it can manipulate or substitute. You can pass on a string to the utility that it interprets and substitute for a commonly recognized set of characters.

The following table summarizes the tr utility’s most common substitutable character sets.

The Linux tr Command Options

You can extend the default functionality of the tr utility by adding a range of flags and options. We have compiled the following table of the most useful options you can use with the tr command.

10 Linux tr Examples to Manipulate Text

Now that you have a clear idea of what tr is and the essential options you can use to transform and manipulate characters, let’s see some examples demonstrating the use of the tr command.

Prerequisites

Before you try the following examples, make sure you have the following:

  • A system running any mainstream Linux distribution.

Example #1: Change Character Case

The most basic use case of the tr utility is to change the case of the characters. The utility offers three ways of changing the case of the input characters.

Option #1: Define the Precise Characters to be Transformed

You can indicate the specific characters in the input that you wish to change to a different case. This option allows for the modification of character case, or the substitution of characters by replacing those in SET1 with corresponding characters in SET2.

Consider the following example where we mentioned the exact characters in SET1 (WWW) and SET2 (www) in the following command:

# cat redswitches | tr WWW www

# cat redswitches tr WWW www

Option #2: Define the Range of Characters for Case Conversion

Defining a specific range of characters enables the tr utility to alter the case of any character that falls within the range.

Here’s an example demonstrating the conversion of uppercase letters to lowercase. In this example command, we defined a range of upper case characters as SET1 ([A-Z]) and lower case characters ([a-z]):

# cat redswitches | tr [A-Z] [a-z]

# cat redswitches tr [A-Z] [a-z]

Remember to press the CTRL and C keys together to exit the input mode.

Also Read: lsof Command in Linux with Examples

Option #3: Specify Interpreted Sequences

Finally, you can identify and transform characters by using the designated interpreted sequences in the command syntax.

For instance, the sequence for lowercase letters is represented by [:lower:], while the sequence for uppercase letters is indicated by [:upper:]. By specifying these two sequences, you can use the tr utility to identify and alter the case of the characters:

# cat redswitches | tr [:upper:] [:lower:]

# cat redswitches tr [upper] [lower]

Example #2: Remove Repeated Characters

A common challenge in manipulating text sequences is to deal with repeated characters, usually multiple white spaces.

The -s option condenses multiple occurrences of a character into a single instance. This feature is especially helpful for converting sequences of whitespace characters into tabs or newline characters when the input text has consecutive whitespace characters.

Consider this example where the input has multiple whitespace characters. The following command converts the spaces in the input character strings with “squeezing”.

# echo “Welcome to RedSwitches” | tr [:space:] ‘\t’

# echo “Welcome to RedSwitches” tr [space] ‘t’

To prevent the inclusion of multiple tabs and whitespaces in the output, add the -s option to the command:

# # echo “Welcome to RedSwitches” | tr -s [:space:] ‘\t’

# # echo “Welcome to RedSwitches” tr -s [space] ‘t’

Example #3: Delete Characters

Utilize the -d option to delete particular characters from the input character string. For instance, consider the following example where tr eliminates every occurrence of the character e:

# echo “Welcome to RedSwitches: \ tr -d ‘e’

# echo “Welcome to RedSwitches tr -d ‘e’

You can optimize this command by choosing a set of characters using designated sequences. For instance, you can easily eliminate all numbers from the input string by using the [:digit:] sequence:

# echo “your Pin is:0007” | tr -d [:digit:]

# echo “your Pin is0007” tr -d [digit]

Example #4: Complement Sets

There are cases when you wish to remove specific characters from the input string. Using a complement set is a great idea where any characters not part of the specified set are eliminated.

In the following example, we applied the -c option to invert the character set in SET1. As you can see, the command removed all characters except for digits:

# echo “your Pin is:0007” | tr -cd [:digit:]

# echo “your Pin is0007” tr -cd [digit]

Example #5: Remove Newline Characters

Newline characters can inflate the space taken by a text file on the disk. The tr utility allows you to compress the text to occupy less space by replacing newline characters with spaces. Note that the entire content of the compressed text is displayed in a single line.

In the following example, we utilize the cat command to access a text file, pipe its output to tr, where we used the -s flag to substitute newline characters with single space characters.

# cat redswitches | tr -s ‘\n’ ‘ ‘

# cat redswitches tr -s ‘n’ ‘ ‘

Example #6: Redirect Into tr

Instead of piping into tr, you can use redirection to input content into the utility. With this approach, you can save the output of the tr command into a file for further processing.

Let’s see a practical application of this idea where we removed all non-printable characters from a file named redswitches, by redirecting its contents into tr:

# cat redswitches | tr -cd [:print:] < redswitches

# cat redswitches tr -cd [print] redswitches

Example #7: Truncate Set Length

When SET1 is longer than SET2, the default behavior of the tr utility is to duplicate the last character of SET2 for processing the input. For instance, consider the example where the tr uses the last character (5) of SET2 to replace characters in SET1:

# echo “Redswitches” \ tr Red 55

# echo “Redswitches” tr Red 55

To address this issue by “truncating” SET1 to match the length of SET2, we recommend using the -t flag. As you can see in the following example command, the -t flag reduced the length of SET1 (reduced to RE) to match the length of SET2 (55):

# echo “Redswitches” | tr -t Red 55

# echo “Redswitches” tr -t Red 55

Example #8: Remove Diacritics

Diacritics are accented characters that can cause issues in processing text input. If you can remove these characters without affecting the context, we recommend trying out the following process.

The tr utility offers the [=CHAR=] sequence to identify and match all characters equivalent to the CHAR. You can use this sequence to detect and eliminate diacritics from input characters.

# cat redswitches | tr “[=e=]” “e”

# cat redswitches tr “[=e=]” “e”

Example #9: Print Each Word Separately

You can use the -cs options to display the contents of a file line by line. In this example, the command substitutes any non-alphanumeric characters with a newline character.

# echo “Welcome to RedSwitches” | tr -cs [:alnum;] ‘\n’

# echo “Welcome to RedSwitches” tr -cs [alnum;] ‘n’

Example #10: Save Output to File

One of the most basic challenges in working with the tr utility is that it does not modify a file’s contents directly. As a result, the changes you make using the utility are not saved.

So, if you wish to save these changes, you need to redirect the output to a file. This involves creative use of redirection, as demonstrated in the following example command:

# tr -cd [:print:] < redswitches > redirect

# cat redswitches tr -cd [print] redswitches

Here, you can see that the command directed the contents of the redswitches file to the tr utility and then directed the output of the command to a new file named redirect.

Also Read: 6 Simple Examples of Using the Linux watch Command

Also Read: A Guide on How to Install and Use Linux Screen Command in 2024

Conclusion

The Linux tr command is particularly useful for text manipulation in Unix-like operating systems.

With tr, you can effortlessly translate characters of the first set (SET1) into those of the second set (SET2). In this process, the utility replaces or deletes characters and even changes the case of the input string.

And for those looking for a hosting provider that complements such powerful command-line tools, RedSwitches is an excellent dedicated web hosting provider choice.

We offer the best dedicated server pricing and deliver instant dedicated servers, usually on the same day the order gets approved. Whether you need a dedicated server, a traffic-friendly 10Gbps dedicated server, or a powerful bare metal server, we are your trusted hosting partner.

FAQ

Q. What is the tr command in Linux?

The tr command in Linux is used to translate or delete characters. It takes input, performs an operation, and gives the output to the standard output.

Q. How can I use the tr command to convert lowercase characters to uppercase?

You can use the command ‘tr a-z A-Z’ to convert lowercase characters to uppercase in Linux.

Q. Can the tr command perform case conversion in Linux and Unix?

Yes, the tr command can perform case conversion in both Linux and Unix. It is a very useful feature of the command.

Q. What is the practical usage of the tr command in Linux with examples?

The tr command is commonly used for tasks like changing the case of characters, deleting specific characters, and translating characters. It can be used with various options and practical examples.

Q. How can I delete specific characters using the tr command?

You can use the tr command with the option ‘-d’ followed by the specific characters you want to delete. For example, ‘tr -d ‘e” will delete all occurrences of the character ‘e’.

Q. What is the purpose of the squeeze option in the tr command?

The squeeze option in the tr command squeezes repeated characters into a single character. It is useful for reducing consecutive duplicate characters to a single character.

Q. How do I remove all digits using the tr command in Linux?

You can use the command ‘tr -d 0-9’ to remove all digits from the input using the tr command in Linux.

Q. In what ways can I learn how to use the tr command in Linux with practical examples?

You can refer to the man page of the tr command, which provides detailed information about its usage, options, and practical examples. You can also explore online resources and tutorials for guidance.

Q. What does the tr command stand for, and what are its common usages?

The tr command stands for “translate.” Its common usages include translating or deleting characters from standard input, performing case conversion, and using character ranges to manipulate the input.

Q. Can the tr command accept input redirection from a file or standard input?

Yes, the tr command can accept input redirection from a file or standard input. This allows you to manipulate the content of a file or process input from other commands. For example, ‘tr a-z A-Z < input.txt’ will perform case conversion on the contents of the input file.

Try this guide with our instant dedicated server for as low as 40 Euros