Linux Tr command with example
The tr
command in Linux is a powerful tool for translating or deleting characters in a text stream. It is a command-line utility that can be used to perform simple string operations on text files, or on the output of other commands.
The tr
command is typically used in conjunction with other commands or scripts to manipulate text data in a variety of ways. It is a versatile tool that can be used for a wide range of tasks, from data cleaning and processing to text manipulation and analysis.
Syntax :
$ tr [OPTION] SET1 [SET2]
1. How to change lowercase to uppercase letters:
You may either select a range of characters or utilise the preset character classes to convert characters from lower case to upper case.
$ cat greekfile | tr [a-z] [A-Z]
To translate white-space characters to tabs using the tr
command, you can use the following command:
$ echo "Welcome To studytonight" | tr [:space:] "\t"
Here, [:space:]
is a character class that represents all white-space characters including space, tab, newline, carriage return, form feed, and vertical tab. The second argument '\t'
is the character that we want to replace the white-space characters with, which in this case is the tab character.
You can use this command with input and output redirection to translate white-space characters in a file or output. For example, to translate white-space characters in a file called input.txt
and save the output to a file called output.txt
, you can use the following command:
We can also use redirection in the preceding example to supply input for tr. Nevertheless, we will utilise a here string this time:
tr [:space:] "\t" <<< "Welcome To Studytonight"
2.With the -s option, you may compress a string of repeating characters:
The -s option is used to compress repeated occurrences of characters given in a set. This eliminates multiple occurrences of characters from the last SET given. Instead, you can turn numerous continuous spaces into a single space.
To squeeze a sequence of repetitive characters using the -s
option with the tr
command, you can use the following command:
$ echo "Welcome To Studytonight" | tr -s " "
3. How to delete specified characters using -d option:
In this example, the echo
command is used to send the string "Example text for command" as input to the tr
command. The -d
option tells tr
to delete characters in the first set specified, To delete specific characters using the -d
option with the tr
command, you can use the following command:
tr -d W <<< "Welcome to Studytonight"
4. To remove all the digits from the string, you can use
In this example, the echo
command is used to send the string "3231
" as input to the tr
command. The -d
option tells tr
to delete characters in the first set specified, which in this case is the [:digit:]
character class that represents all digits (0-9).
Output:
$ echo "my ID is 3231" | tr -d [:digit:]
5. How to complement the sets using -c option
The -c
option in the tr
command can be used to complement a set of characters. This means that it will remove all characters that are not present in the specified set. For example, if you want to remove all characters from a string except for digits, you can use the following command:
$ echo "my ID is 92131212" | tr -cd [:digit:]
Conclusion
The tr
command in Linux is a very useful tool for translating, deleting, and squeezing characters in a file or stream of input. It can be used to perform simple text processing tasks such as changing the case of characters, replacing characters, and removing specific characters from input.