10 ways to unleash the power of the linux ‘tr’ command

10 Ways to Unleash the Power of the Linux ‘tr’ Command

When you want to translate characters in and with style, checkout this in-depth guide to the Linux `tr` command!

Keywords: Linux, command line, bash, tr, system administration, shell scripting, UNIX utilities

Introduction

Welcome, command line aficionados! Today, we explore the realm of the `tr` command, an unsung hero of the Linux world. Like a master linguist, `tr` stands for translate or transform, tirelessly converting, deleting, and squeezing characters in your input. So, strap in as we embark on a thrilling journey to discover the power of the `tr` command.

The Syntax of `tr`

The `tr` command reads from the standard input, transforms the content in some way, and then sends it to the standard output. A simple yet powerful tool, `tr`’s syntax can be expressed as:

$ tr [options] set1 [set2]

Here, `options` are the parameters we can use with `tr`, `set1` and `set2` are character sets. `tr` translates or transforms characters from `set1` into corresponding characters from `set2`.

Example 1: Converting Case

First on our agenda is case conversion. With `tr`, switching from lowercase to uppercase (and vice versa) is a breeze:

$ echo 'Hello World' | tr 'a-z' 'A-Z'

In this example, `tr` translates all lowercase letters to uppercase. Suddenly, our greeting has a lot more emphasis!

Example 2: Deleting Characters

Next, let’s see how `tr` can delete specific characters from the input. Want to remove all digits from a text? `tr` to the rescue:

$ echo '123Hello456' | tr -d '0-9'

By using the `-d` option, we instruct `tr` to delete all occurrences of the characters in the set, in this case, digits from 0-9.

Exploring `tr` Options

Just like a seasoned explorer, `tr` comes equipped with a range of options to conquer various tasks. Let’s dive into some of the most useful ones:

Option `-s`: Squeezing Repeats

The `-s` option squeezes or reduces sequences of identical characters in the input to a single character:

$ echo 'Heelllooo Wooorlld' | tr -s 'a-z'

With `-s`, `tr` squeezes the repeated lowercase characters into a single character, transforming enthusiastic greetings into normal ones.

Option `-c` or `-C`: Complementing the Set of Characters

The `-c` or `-C` option complements the set of characters, meaning `tr` will consider all characters not listed in the set:

$ echo 'Hello 123 World' | tr -d -c 'A-Za-z \n'

This command tells `tr` to delete all characters that are not uppercase or lowercase letters, spaces, or newlines. It’s like having a filter for your text!

Example 3: Transforming Spaces into Tabs

Ever needed to replace all spaces in a text with tabs? `tr` has got you covered:

$ echo 'Hello     World' | tr ' ' '\t'

This command instructs `tr` to replace all spaces with tabs. It’s as easy as a tab, er, snap!

Example 4: Reducing Multiple Spaces

Want to clean up a text littered with extra spaces? With `tr`, it’s a walk in the park:

$ echo 'Hello     World' | tr -s ' '

This command squeezes multiple spaces into a single space. Now, that’s a clean-up operation!

Example 5: Deleting Non-Printable Characters

If a file contains non-printable characters, `tr` can help you get rid of them:

$ tr -cd '\11\12\15\40-\176' < file-with-non-printable-chars > clean-file

This command removes all non-printable characters from the file. It’s like a cleaning service for your files!

Example 6: Translating White Spaces to Newlines

Want to translate all white spaces to new lines? Here’s how:

$ echo "Hello World This Is A Test" | tr ' ' '\n'

This command translates all spaces in the input to new lines, placing each word on its own line. Perfect for vertical reading!

Example 7: Scrambling Letters

You can use `tr` to perform simple letter substitution ciphers, like ROT13:

$ echo "Hello World" | tr 'A-Za-z' 'N-ZA-Mn-za-m'

This command performs a ROT13 transformation on the text, a simple encryption method that replaces a letter with the 13th letter after it in the alphabet.

Example 8: Stripping Out Non-ASCII Characters

If you need to strip out non-ASCII characters from a file, `tr` can help:

$ tr -cd '\0-\177' < inputfile > outputfile

This command removes all non-ASCII characters from `inputfile` and writes the result to `outputfile`. It’s a useful tool for cleaning up text files!

Example 9: Converting DOS/Windows line endings to Unix

Line endings can cause headaches when transferring text files between DOS/Windows and Unix/Linux systems. Here’s how `tr` can help:

$ tr -d '\r' < windows.txt > unix.txt

This command removes the carriage return characters (`\r`) that Windows uses for line endings, leaving only the line feed (`\n`) that Unix expects.

Example 10: Creating a list of all words in a text

You can use `tr` to convert a text into a sorted list of words:

$ tr -cs "A-Za-z" '\n' < input.txt | sort | uniq > wordlist.txt

This command changes all non-letter characters into newlines, sorts the resulting words, and removes duplicates. The result is a sorted list of all unique words in the text.

Conclusion

From changing case to deleting characters, we’ve traversed the transformative terrain of the `tr` command. With `tr` in your command line toolkit, you’re prepared to tackle text transformations like a pro. Remember, the key to mastering the command line is practice, so don’t hesitate to `tr` it out! Until our next command line chronicle, happy learning!