Polecenia Linuksa

Linux tr Command

Linux tr Command
In Linux, “tr” is a built-in tool that can “translate, squeeze, and/or delete characters from standard input, writing to standard output” (from man page).

From the official description, it's easy to understand the value of this tool. Linux comes up with a big collection of built-in tools. There are some special ones that are super useful for text manipulation. We've already covered a number of those, like Vim, Nano, awk, sed and other tools.

Those who work with texts on a regular basis, “tr” is definitely going to be really, really useful. This article will illustrate the most common usage of “tr” with sufficient examples.

Note: The tutorial is going to use a ton of dummy files with random content. All the random strings are generated by Random.org.

Location

which tr

This is the full path of the “tr” command tool.

Basic usage

For using “tr” tool, you have to use the following command structure.

tr [SET1] [SET2]

There are different options and ways to manipulate the texts using “tr”. At first, let's check out this demo file.

cat demo.txt

All the characters are in lowercase, right? Let's transform them to uppercase!

cat demo.txt | tr [:lower:] [:upper:]

Here, the first parameter of “tr” is indicating to perform a translation on all the lowercase characters of the input. The second part is telling to transform them into uppercase at the output.

Now, let's do the opposite.

cat demo1.txt

cat demo1.txt | tr [:upper:] [:lower:]

There's also another way of performing this same task. Let's check it out.

cat demo.txt | tr [a-z] [A-Z]

Now, instead of telling “tr” to translate uppercase to lowercase or lowercase to uppercase, we told to identify entries matching the range “a” to “z” and translate into their equivalent from the range “A” to “Z”.

This similar method can also be used to translate uppercase to lowercase.

cat demo1.txt | tr [A-Z] [a-z]

Playing with numbers

We saw how to translate uppercase to lowercase, right? It's time to play around with digits.

Using the following command, we can easily translate all the digits (0-9) into their equivalent characters!

cat demo_digit.txt

cat demo_digit.txt | tr [0-9] [a-z]

Awesome! How about uppercase ones?

cat demo_digit.txt | tr [0-9] [A-Z]

Simple, yet interesting, right? We can also transform characters to digits as well!

cat demo_lowercase.txt

cat demo_lowercase.txt | tr [a-z] [0-9]

Uh oh, seems like things broke down, right? Well, we can only use 'a' to 'j' to represent single digits. If there's any character that's out of this bound, “tr” will replace the character with ']'.

Deleting characters

As the official description suggests, “tr” can do more than just translation of characters. In the following example, we'll check out how to use “tr” to delete certain characters.

cat random.txt

Now, let's delete all the numbers from the content.

cat random.txt | tr -d [0-9]

Here, the “-d” flag is to tell “tr” to delete and [0-9] denoting digits to delete.

We can also do that with the characters. The following command will keep all the numbers but remove all the characters.

cat random.txt | tr -d [a-z]

How about removing just a single specific character from the file?

cat demo_lowercase.txt

cat demo_lowercase.txt | tr -d 'y'

It deleted all the 'y' entries from the file.

Squeezing repeated characters

There are times when a character is repeated sequentially. If you're dealing with such annoying issues, just let “tr” take care of it! Using the following command, you can squeeze such occurrences. It essentially keeps the initial occurrence of the character and deletes the additional ones.

First, time to check how the demo file looks.

cat duplicate.txt

Now, pass the content to “tr”.

cat duplicate.txt | tr -s [:digits:]

Here, the “-s” parameter is the indicator for performing the “squeeze” action.

Breaking/merging sentences

Let's check out the demo file.

bat duplicate.txt

It contains a sentence with spaces dividing the words, right? Let's split the words into new lines.

cat duplicate.txt | tr " " "\n"

This command replaced all the space characters with newline characters.

Let's merge the broken sentence into a long sentence again.

bat duplicate.txt

cat duplicate.txt | tr "\n" " "

Translating characters

Just like we translated characters before, this time, we'll do the same but with a single character only.

cat demo.txt

In this file, let's change all the newline characters into a semicolon.

cat demo.txt | tr "\n" ";"

I had to wrap it around “echo”. Otherwise, it would create some output because the last newline character would also be translated into a semicolon.

There's a different way of changing characters. However, this one is harder to control.

cat duplicate1.txt

cat duplicate1.txt | tr -c 'd' 'z'

cat duplicate1.txt | tr -c 'd' 'z'

Wow! Let's wrap it around “echo” to better understand the output.

echo $(cat duplicate1.txt | tr -c 'd' 'z')

What happened here? Using the “-c” flag, “tr” will only keep the target character unchanged. In the case of mismatch, every other character will be transformed. Here, any character other than 'b' was replaced by 'z'.

Translating strings

“tr” can also work with strings. Let's perform string replacement.

cat duplicate1.txt

cat duplicate1.txt | tr "duplicate" "not_duplicate"

My string to replace is shorter than the string to be replaced with, so it didn't fit.

Character sets

By now, you've noticed that there are a number of character sets supported by “tr”. While a number of them were used in the above examples, other characters sets are also quite helpful. Here's a list of all the supported character lists.

POSIX character sets

  • [:digit:] : Digits 0-9
  • [:alpha:] : Alphabets a-z and A-Z.
  • [:alnum:] : Alpha-numeric characters
  • [:punct:] : Punctuation symbols
  • [:space:] : Any whitespace character, for example, space, tab, FF, CR, NL, FF, etc.
  • [:upper:] : All uppercase alphabets
  • [:lower:] : All lowercase alphabets
  • [:cntrl:] : All control characters (NL, CR, EXT, STX, ACK, SO, DC1, DC2, NAK, ETB, ESC, IS1, IS2, DEL etc.)

Additional character sets

  • [A-Z] : All uppercase alphabets
  • [a-z] : All lowercase alphabets
  • [0-9] : All digits

Final thoughts

There are tons of ways all these features of “tr” can benefit the users. I always recommend to check out all the available options and in-depth guides on any Linux tool from their man, info and help pages as they can offer more valuable knowledge.

tr --help

man tr

info tr

Enjoy!

Gry Najlepsze dystrybucje Linuksa do gier w 2021 r
Najlepsze dystrybucje Linuksa do gier w 2021 r
System operacyjny Linux przeszedł długą drogę od pierwotnego, prostego, serwerowego wyglądu. Ten system operacyjny znacznie się poprawił w ostatnich l...
Gry Jak przechwytywać i przesyłać strumieniowo sesję gry w systemie Linux
Jak przechwytywać i przesyłać strumieniowo sesję gry w systemie Linux
W przeszłości granie w gry było uważane tylko za hobby, ale z czasem branża gier odnotowała ogromny wzrost pod względem technologii i liczby graczy. P...
Gry Najlepsze gry do grania ze śledzeniem rąk
Najlepsze gry do grania ze śledzeniem rąk
Oculus Quest niedawno wprowadził świetny pomysł śledzenia rąk bez kontrolerów. Przy stale rosnącej liczbie gier i działań, które wspierają zarówno for...