15 Linux tr command Examples Translate, Squeeze, And Delete Characters
The tr command is a fairly useful command to manipulate text in files. Translate, squeeze, and/or delete characters from standard input, writing to standard output. Name tr is often regarded as an acronym for translate due to the nature of its functionality as it translates its input text to the intended output text. Features provided by tr are very similar to that of sed but without much of a learning curve.
Some of the common uses of the tr command include case conversion, removal of duplicate characters in a file, joining lines in a file etc.
In this article, we will go through some examples to help you better understand how we could use tr to fulfill our text translation needs.
The basic syntax for the tr command is as follows:
tr [OPTION] SET1 [SET2]
SET1 denotes what we wish to translate in the input and SET2 denotes what we wish to translate SET1 as the output of the translation.
Example1: tr command Convert all characters in input from lower to upper case.
[root@sahil-centos ~]# echo "sahil suri" | tr 'a-z' 'A-Z' SAHIL SURI
The tr command mentioned above implies to translate all alphabets in lowercase denoted by ‘a-z’ into alphabets in upper case denoted by ‘A-Z’
Example2: Convert all characters in input from upper to lower case.
In this example, we perform exactly the reverse action of the previous example.
[root@sahil-centos ~]# echo "SAHIL SURI" | tr 'A-Z' 'a-z' sahil suri
Example3: Use multiple ranges while performing a translation operation.
We are limited to using a single range of characters while working with the tr command. As you will observe in the below example, we have two ranges together in the same tr command.
[root@sahil-centos ~]# echo "sahil suri" | tr 'a-dr-z' 'A-DR-Z' SAhil SURi
Above example, takes characters a to d and also characters between r and z and changes them from lower to upper case in the input text.
Example4: Translate braces into parenthesis.
Need to convert a type of brackets to a different type while working with code or scripts. This can easily be done using the tr command.
Given below is an example.
[root@sahil-centos ~]# echo "{ Hello World }" > in.txt [root@sahil-centos ~]# tr '{}' '()' < in.txt > outfile [root@sahil-centos ~]# cat outfile ( Hello World )
Example5: Translate spaces to new lines
While working with text files, we frequently come across requirements where we need to convert spaces to something else.
[root@sahil-centos ~]# cat in.txt centos ubuntu fedora [root@sahil-centos ~]# cat in.txt | tr " " "\n" centos ubuntu fedora [root@sahil-centos ~]# cat in.txt | tr "[:space:] " "\n" centos ubuntu fedora
While using translate you may use ” ” or [:space:] to represent white space. I personally find the first option easier to type.
Example6: Replace white space with a different delimiter
Instead of converting to a new line, we could convert the space between characters to a different delimiter like a colon(:) or a pipe(|) symbol.
[root@sahil-centos ~]# cat in.txt | tr " " ":" centos:ubuntu:fedora [root@sahil-centos ~]# cat in.txt | tr " " "|" centos|ubuntu|fedora
Example7: Replace multiple occurrences of a character with a single occurrence of the character
In this example, we’ll you how to remove duplicate occurrences of a character and replace them with a single occurrence of that character.
Consider the file in.txt.
[root@sahil-centos ~]# cat in.txt centos ubuntu fedora
From the above output, it’s clear that there is an uneven number of whitespace characters between the three words. To replace these multiple spaces with a single space, we use the -s or the squeeze option with the tr command as shown below.
[root@sahil-centos ~]# tr -s " " < in.txt centos ubuntu fedora
This replaces multiple occurrences of a character with a single occurrence of the same character.
Example8: Replace multiple occurrences of a character with a different character
Building up from the previous example, in this example we use the squeeze option provided by tr to replaces multiple spaces with a single semicolon character.
[root@sahil-centos ~]# cat in.txt centos ubuntu fedora [root@sahil-centos ~]# tr -s " " ";" < in.txt centos;ubuntu;fedora
Example9: Delete all occurrences of a character
To delete all occurrences of a character we use the -d option with the tr command followed by the character to be removed.
[root@sahil-centos~]# echo "linuxhp-uxaix" | tr -d 'x' linuhp-uai
As shown above example removes all occurrences of the character x from the input string.
Example10: Remove all digits or all alphabets from the input
Providing the range 0-9 to the tr command while using the -d option will remove all digits from the input
[root@sahil-centos ~]# echo "james bond 007" | tr -d '0-9' james bond
Example11: Remove all alphabets from the input
Providing a range of a-z accompanied by A-Z will remove all alphabetic characters from the input.
[root@sahil-centos ~]# echo "james bond 007" | tr -d 'a-zA-Z' 007
Example12: Remove all characters except the given set with tr
We used -d option to delete characters, mentioned from the input. We may use the -c option with the -d option to delete all characters except those that mention.
[root@sahil-centos ~]# echo "centos ubuntu hp-ux" | tr -cd 'sh' sh
The above tr command removed all characters including the new line leaving behind the characters s and h which we mentioned to be removed. Using the -c option is also sometimes referred to as complimenting the set.
Example13: Remove all non-alphanumeric and space characters
We can use the -c and -d options we used in the previous example to remove any character which is not an alphabet, digit or white-space as shown
[root@sahil-centos ~]# echo "centos | ubuntu 007 ; hp-ux :::: fedora" | tr -cd "[:alpha:][:space:][:digit:]" centos ubuntu 007 hpux fedora
This example could prove to be especially useful when working with a file having a lot of junk characters
Example14: Translate non-alphanumeric characters into a single newline character
Using the -c option to compliment in conjunction with the -s option to squeeze, we could convert print every word separated by spaces in a file in a new line by translating non-alphanumeric characters into newline characters.
[root@sahil-centos ~]# echo "james bond 007" | tr -cs 'a-zA-Z0-9' '\n' james bond 007
Example15: Join all lines in a file into a single line
The below tr command squeezes each occurrence of the new line characters into a single space character. Thus, our text file consisting of three lines becomes a text file with a single line with the words being separated by a single space character. This option can be useful when attempting to consolidate scattered text in a file.
[root@sahil-centos ~]# cat in.txt centos ubuntu fedora [root@sahil-centos ~]# [root@sahil-centos ~]# tr -s '\n' ' ' < in.txt centos ubuntu fedora
Conclusion
This concludes our discussion of the tr command. We hope that the examples illustrated in this article encourage you to adopt using more of the tr command in your daily tasks.
SETs are specified as strings of characters. Most represent themselves. Interpreted sequences are
\NNN character with octal value NNN (1 to 3 octal digits) \\ backslash \a audible BEL \b backspace \f form feed \n new line \r return \t horizontal tab \v vertical tab
Related Articles
Thanks for your wonderful Support and Encouragement