13 Linux tr command practical examples you should know as a system administrator.
The tr command short for translate, is one of the most useful command for manipulating text on the command line.
It allows you to perform useful operations such as converting lowercase characters to uppercase characters, uppercase characters to lowercase characters, character replacing, and deleting characters. It is usually used in conjunction with other commands via piping.
In this article, I will show you 13 practical examples of the tr command in Linux. So letโs jump right into it.
๐ญ. ๐๐ฒ๐น๐ฒ๐๐ถ๐ป๐ด ๐ฟ๐ฒ๐ฝ๐ฒ๐ฎ๐๐ฒ๐ฑ ๐ฐ๐ต๐ฎ๐ฟ๐ฎ๐ฐ๐๐ฒ๐ฟ๐
You can use the -s option to squeeze a character that is repeating to make it a single character.
This option is handy when squeezing multiple continuous space characters into a single character.
$ echo "Linux is awesome, I'm in love with it." | tr -s " "
Instead of using the space character in our character SET for squeezing repeating characters, we can also make use of character classes and still get the same results.
$ echo "Linux is awesome, I'm in love with it." | tr -s "[:space:]"
๐ฎ. ๐๐ฒ๐น๐ฒ๐๐ฒ ๐๐ฝ๐ฒ๐ฐ๐ถ๐ณ๐ถ๐ฐ ๐ฐ๐ต๐ฎ๐ฟ๐ฎ๐ฐ๐๐ฒ๐ฟ๐
Using the -d option, you can delete the characters you specify. tr command deletes every instance of the "-" character in the following example:
$ echo "Some- people- are- afraid- to- use -the Linux -System." | tr -d "-"
๐ฏ. ๐๐ฒ๐น๐ฒ๐๐ฒ ๐ฎ๐น๐น ๐๐ต๐ฒ ๐ฑ๐ถ๐ด๐ถ๐๐
You can also use the -d option to remove any numbers or digits from your text. $ echo "Some people are afraid to use the Linux System. 2023" | tr -d "[:digit:]"
Instead of character classes, you can use number character range and get the same results.
$ echo "Some people are afraid to use the Linux System. 2023" | tr -d "0-9"
๐ฐ. ๐๐ฎ๐๐ฒ ๐ฐ๐ผ๐ป๐๐ฒ๐ฟ๐๐ถ๐ผ๐ป
The tr command is frequently used to convert lowercase letters to uppercase letters or the opposite.. The character class [:lower:] matches all lowercase characters, while the character class [:upper:] matches all uppercase characters.
The following will transform characters from upper case to lower case.
$ echo "Some people are afraid to use the Linux System." | tr "[:lower:]" "[:upper:]"
Alternatively, you can also use character range (regular expression) in place of the character classes.
$ echo "Some people are afraid to use the Linux System." | tr "a-z" "A-Z"
๐ฑ. ๐ฅ๐ฒ๐บ๐ผ๐๐ถ๐ป๐ด ๐ป๐ผ๐ป ๐ฎ๐น๐ฝ๐ต๐ฎ๐ป๐๐บ๐ฒ๐ฟ๐ถ๐ฐ๐ฎ๐น ๐ฐ๐ต๐ฎ๐ฟ๐ฎ๐ฐ๐๐ฒ๐ฟ๐
We can combine the complement option (-c) with the delete option (-d) to delete all non-alphanumerical characters.
The following command will delete all non-alphanumerical characters
$ echo "I: have- been@ using# Linux for 12 years." | tr -cd "[:alnum:]"
๐ฒ. ๐ฃ๐ฟ๐ถ๐ป๐ ๐ฒ๐ฎ๐ฐ๐ต ๐๐ผ๐ฟ๐ฑ ๐ผ๐ป ๐ฎ ๐๐ถ๐ป๐ด๐น๐ฒ ๐น๐ถ๐ป๐ฒ
The $PATH variable is an environmental variable that contains a list of directories separated by a colon that instructs the shell where to look for executable files when you type a command.
So if you're having trouble reading the directories in the $PATH variable, you can make use the tr command to replace the colons with the newline characters so each directory is displayed on a single line.
$ echo $PATH | tr ":" "\ n"
The preceding example will replace all the semicolons in our path variable with the newline characters.
๐ณ. ๐ฅ๐ฒ๐บ๐ผ๐๐ฒ ๐ฎ๐น๐น ๐ป๐ผ๐ป-๐ป๐๐บ๐ฒ๐ฟ๐ถ๐ฐ ๐ฐ๐ต๐ฎ๐ฟ๐ฎ๐ฐ๐๐ฒ๐ฟ๐
The -c option instructs tr to use the complement in the SET given. In this example, we want to remove all of the letters and only keep the phone number.
$ echo "Call me at +449883200382" | tr -cd "[:digit:]"
This is very useful for extracting phone numbers or employee IDs from text files.
๐ด. ๐ฅ๐ฒ๐บ๐ผ๐๐ฒ ๐ก๐ฒ๐๐น๐ถ๐ป๐ฒ ๐๐ต๐ฎ๐ฟ๐ฎ๐ฐ๐๐ฒ๐ฟ๐
Assume you have a text file containing data that looks like this, and you want to remove the newlines and put the words on a single line separated by spaces.
$ cat file.txt
To achieve that, you can redirect your file contents to the tr command as shown in the below command.
$ tr "\ n" " " < file.txt This will replace each newline character in a text file with a space.
๐ต. ๐ฃ๐๐ ๐ฒ๐ฎ๐ฐ๐ต ๐๐ผ๐ฟ๐ฑ ๐ถ๐ป ๐ฎ ๐ป๐ฒ๐ ๐น๐ถ๐ป๐ฒ
As a system administrator, you may be given employee names on single lines separated by spaces, and you may want to put each name on a single line for easy readability.
The command below will assist you in accomplishing this by dividing a sentence into multiple lines, with each word on its line.
$ echo "john james kay fredrick george" | tr "[:space:]" "\ n"
๐ญ๐ฌ. ๐๐ผ๐ป๐๐ฒ๐ฟ๐ ๐ฎ ๐ณ๐ผ๐ฟ๐๐ฎ๐ฟ๐ฑ ๐๐น๐ฎ๐๐ต (/) ๐๐ผ ๐ฎ ๐ต๐๐ฝ๐ต๐ฒ๐ป๐ (-).
As a system administrator, you may be tasked with changing the date formats from yyyy/mm/dd to the new format yyyy-mm-dd.
Here's an example of converting a forward slash (/) to a hyphen (-) and then appending the data to a file for storage.
$ tr "/" "-" < old-date-format.txt > new-date-format.txt
๐ฆ๐๐บ๐บ๐ถ๐ป๐ด ๐จ๐ฝ!
This short newsletter demonstrated how to use the tr command with practical examples and some available options for various text transformations. If you get stuck with this command, feel free to refer to the man pages or the command help menu.
Thatโs all! Hope you learn something new from this article. If so, please let me know by liking it. If you're new here, do subscribe to my newsletter for more threads, tips, and resources on Linux, DevOps, and sysadmin.