Mastering the Linux tr Command in 8 Simple Steps

Mastering the Linux tr Command in 8 Simple Steps

Learn how to manipulate text in Linux with ease using the powerful tr command Replace, delete, combine, and compress input text quickly and without any fuss Discover the many functions of tr, including using ranges and tokens, inverting matches, and even combining and splitting lines Save time and streamline your text manipulation workflow today

The tr command is a versatile and efficient tool for manipulating text streams in Linux. With the ability to substitute, delete, or convert characters based on specified rules, tr can quickly and easily modify input text for a variety of purposes. Whether you need to remove unwanted characters, combine text, or compress data, tr can save you time and effort. Here's how it works.

What Is the tr Command?

Tracing its roots back to Unix, the Linux tr command is a powerful utility for manipulating text streams. Its name comes from its ability to "translate" text based on user-defined rules. Although Linux has added its own features and functionality, it adheres to the Unix philosophy and design principles. This includes the concept of programs that do one thing well and generate output that can be used by other programs. With tr, you can easily transform text streams to suit your needs, whether you are working with command line tools or streaming text from a file. The output stream can be directed to other programs or multiple instances of tr for further processing.

The tr command has the ability to substitute characters in its input stream based on set rules. Typically, tr commands require two sets of characters: one set to hold the characters that will be replaced if found in the input stream, and another set to hold the characters they will be replaced with. The replacement works by replacing the first character in set one with the first character in set two, the second character in set one with the second character in set two, and so on. For example, using the command "tr 'c' 'z'" will replace all occurrences of the letter "c" with "z" in the input stream. It's important to note that tr is case-sensitive. To illustrate, using the echo command, "abcdefabc" is pushed into tr and "c" is replaced with "z". The resulting output is displayed in the terminal window.

When searching for the letters "a" and "c," it's important to note that we're searching for them individually and not as a combination. To replace any occurrence of "a" with "x" and "c" with "z," use the command "echo abcdefabc | tr 'ac' 'xz'." It's important to have the same number of characters in both sets for this to work properly. If there are more characters in set one than in set two, the extra characters will be replaced with the last character in set two. To prevent this, use the -t (truncate) option to only replace characters that have a matching character in set two. Keep in mind that the output must always include the placeholders

Mastering the Linux tr Command in 8 Simple Steps

and

Mastering the Linux tr Command in 8 Simple Steps

.

Using Character Ranges to Change the Case of a Text Stream

Character ranges can be used in sets to represent a range of characters. For instance, [a-z] refers to all lowercase letters, while [A-Z] refers to all uppercase letters. This feature can be utilized to change the case of a text stream.

To convert the input stream to uppercase, use the following command:

echo "How-To Geek" | tr '[a-z]' '[A-Z]'

Mastering the Linux tr Command in 8 Simple Steps

On the other hand, to flip the case, simply swap the uppercase and lowercase ranges in the command:

echo "How-To Geek" | tr '[A-Z]' '[a-z]'

Mastering the Linux tr Command in 8 Simple Steps


We can utilize various tokens in order to match with common cases. These tokens include:

- [:alnum:], which matches with letters and digits.

- [:alpha:], which matches with letters exclusively.

- [:digit:], which matches with digits only.

- [:blank:], which matches with tabs and spaces.

- [:space:], which matches with all whitespace, including newline characters.

- [:graph:], which matches with all characters, including symbols, but not spaces.

- [:print:], which matches with all characters, including symbols and spaces.

- [:punct:], which matches with all punctuation characters.

Using tokens, we can easily convert lowercase letters to uppercase and vice versa.

Example:

echo "How-To Geek" | tr '[:lower:]' '[:upper:]'

echo "How-To Geek" | tr '[:upper:]' '[:lower:]'

To match all characters except for those in the first set, we can use the -c (complement) option. For instance, the following command replaces everything except for the letter "c" with a hyphen "-".

echo abcdefc | tr -c 'c' '-'

Mastering the Linux tr Command in 8 Simple Steps

Inverting the Matches

To modify the input stream, the tr command can be used. In this example, the command adds the letter "a" to the first set and converts anything aside from "a" or "c" to a hypen "-".

To remove characters entirely, the -d (delete) option can be used. This command removes any occurrence of "a", "d", or "f" from the input stream.

When only one set of characters is present on the command line, we have a unique instance. The -s (squeeze-repeats) option is another case where only one set of characters is used, reducing repeated characters to a single character. For example, this option can reduce repeated sequences of the space character to a single space. It's worth noting that the [:blank:] token represents the space character, while the [:space:] token represents all forms of whitespace, including tabs and newline characters. However, we can replace [:blank:] with [:space:] and obtain the same result.

Mastering the Linux tr Command in 8 Simple Steps

For instance, we can execute the following command to reduce repeated sequences of whitespace characters to a single space:

echo "abc de f c" | tr -s '[:space:]'

Mastering the Linux tr Command in 8 Simple Steps


When it comes to deleting characters, the differences between [:blank:] and [:space:] become apparent. By using the -d (delete) option and providing a set of characters for tr to look for in its input stream, any found characters will be removed. For example, if we use tr to delete [:blank:] in the string "abc de f c", the spaces will be deleted and a newline will be added after the output stream is written in the terminal window. However, if we use [:space:] instead of [:blank:], any spaces, tabs, and newline characters will be removed from the input stream without starting a new line after the output. It's important to note that [:space:] includes newlines, but you could also use an actual space character to achieve the same result.

Deleting Characters

Mastering the Linux tr Command in 8 Simple Steps

Mastering the Linux tr Command in 8 Simple Steps


To remove all spaces from a string, we can use the "tr" command with the -d option and specify the space character. Here's an example:

echo "abcde f c" | tr -d ' '

Mastering the Linux tr Command in 8 Simple Steps

Alternatively, we can delete all digits from a string by using the "tr" command with the -d option and the [:digit:] character class.

echo abcd123efg | tr -d '[:digit:]'

Mastering the Linux tr Command in 8 Simple Steps

To delete everything except digits, we can combine the -c (complement) and -d (delete) options and specify the [:digit:] character class.

echo abcd123efg | tr -cd '[:digit:]'

Mastering the Linux tr Command in 8 Simple Steps


If we replace spaces with newline characters, each word in a line of text can be placed on its own line.

echo 'one two three four' | tr ' ' '\n'

To substitute colons for spaces, we can use the following command:

echo 'one two three four' | tr ' ' ':'

To split difficult-to-read text into easier-to-manage output, we can find the delimiter in use and replace it with newline characters. In the case of the path environment variable, the delimiter is a colon, so we can replace it with newline characters:

Remember the output MUST ALWAYS KEEP/INCLUDE placeholder(s) :

Combining and Splitting Lines

Mastering the Linux tr Command in 8 Simple Steps

Mastering the Linux tr Command in 8 Simple Steps


To make the output more readable, use the command "echo $PATH | tr ":" "\n"". This will separate each path in the output onto its own line.

To reformat text into a single line, use the command "cat lines.txt | tr '\n' ' '". This will convert each line of text in the file "lines.txt" into a single line, with each word separated by a space.

Mastering the Linux tr Command in 8 Simple Steps

Mastering the Linux tr Command in 8 Simple Steps


Using tr With Pipes

The input from tr can be utilized by other programs, or even by tr itself. This command involves the use of tr four times. The first tr removes any hyphens “-” from the input, followed by the second tr which condenses repeated spaces into a single space. The third tr changes spaces to underscore “_” characters, and the fourth and final tr converts the string to lowercase. To illustrate: echo "Mangled FiLE-nAMe.txt" | tr -d '-' | tr -s ' ' | tr ' ' '_' | tr '[:upper:]' '[:lower:]'

Mastering the Linux tr Command in 8 Simple Steps


Simple Is as Simple Does

Tr is a simple command that requires minimal learning and memorization. It is a great tool that often allows you to accomplish your tasks without the need for more complex tools like sed. However, if you find yourself building a long chain of commands with tr and still struggling to achieve your desired result, it may be time to consider using sed instead.