Updated May 29, 2023
Introduction to Linux tr Command
The Linux tr command is a command-line utility. It will translate and/or delete the characters from standard input (input file or data) and then write the output to standard output. For manipulating the input text data, the Linux tr command will more beneficial. The Linux tr command-line utility was written by Jim Meyering.
Syntax:
tr [OPTION]... SET1 [SET2]
- tr: Using the tr keyword in the command | syntax. It will take the two sets of characters as SET 1 and SET 2 and replace or delete the characters as per the requirement|option.
- OPTION: We can provide the different flags as an option to tr command.
- SET 1 and SET 2: It is translating the character from SET 1 to SET 2.
How does Linux tr Command Work?
The Linux tr command or utility is reading the byte stream from its input data and writes the resulted output (standard output). The SET 1 and SET 2 are the two inputs arguments (the same length). It will replace the characters in the SET 1 with respective SET 2.
Examples to Implement of Linux tr Command
Below are the examples of Linux tr Command:
Example #1 – Convert Lower Case to Upper Case
In tr command, there is a utility to convert the lower case to upper. The set [:lower:] will match any set of the lower case. The set [:upper:] will match any set of the upper case. The set [:lower:] [:upper:] is useful to convert the lower case to the upper case.
Command:
cat file.txt
cat file.txt | tr '[:lower:]' '[:upper:]'
Explanation:
We are having a sample directory. In the same directory, we have the “file.txt” file in it. In “f1.txt” file, we are having some data in it (refer screenshot 1 (a)). We are reading the file and with the help of pipe operation, we are providing the same data as an input to the command. With the help of the above tr command we are converting the input data from lower case to upper case (refer screenshot 1 (c)).
Output:
Screenshot 1 (a)
Screenshot 1 (b)
Example #2 – Complement Pattern Search
In the tr command, with the help of the “-c” option we can match the SET 1 character with input data and replace all the input data with the SET 2 character.
Command:
cat file.txt
cat file.txt | tr -c 'e' 'z'
Explanation:
In the above command, we are comparing input data with two SET 1 (“e”) and SET 2 (“z”). in output, if the SET 1 character is present in the input file. the same character will present as it is. Except SET 1 character all the input data will replace by SET 2 character.
Output:
Example #3 – Delete Specific Character
In tr command, we are able to delete the specific words or characters from the input data or file. We need to use the “-d” option in the tr command.
Command:
cat file.txt
cat file.txt | tr -d 'to'
Explanation:
In the above command, we can able to delete the unwanted character or word. In the above command, we need to delete the “to” word from the input data. In the output, the “to” word was deleted with the help of the “-d” option.
Output:
Example #4 – Remove Unwanted Space
In tr command, we can delete or remove the unwanted space for the input data. We can use the “-s” option with “[:space:]” keyword in the tr command to remove the unwanted spaces.
Command:
cat file.txt
cat file.txt | tr -s '[:space:]'
Explanation:
In the input file “file.txt”, there are lots of spaces in the file. In the above command, we use the “-s” option to remove the unwanted spaces. In the output, it will remove the unwanted spaces and display the data.
Output:
Example #5 – Remove Digit
In the Linux tr command, we are able to remove the decimal values or digit from the input file or data. We need to use the “-d” option with “[:digit:]” keyword in the tr command.
Command:
cat file.txt
cat file.txt | tr -d [:digit:]
Explanation:
For the input file, we are having the file.txt file. It is containing few text data as well as numeric data. We need to remove all the digit or numeric values form the input file. With the help of “cat” command, we are reading the input file data and providing as an input to tr command (via Linux pipe utility). In the above command, we have used the “-d” option with [:digit:] keyword. It will help to remove all the numeric or digit value from the input file.
Output:
Example #6 – Remove Character
In Linux tr command, we are able to remove the character or character string from the input file or data. We need to use the “-cd” option with “[:digit:]” keyword in the tr command.
Command:
cat file.txt
cat file.txt | tr -cd [:digit:]
Explanation:
For the input file, we are having the file.txt file. It is containing few text data as well as numeric data. We need to remove all the character or character string from the input file. With the help of “cat” command, we are reading the input file data and providing as an input to tr command (via Linux pipe utility). In the above command, we have used the “-cd” option with [:digit:] keyword. It will help to remove all the character or character string from the input file and keep the digit or numeric values as it is.
Output:
Example #7 – DeleteSpecific Character
In Linux Tr command, we are having the functionality to remove the specific or wanted character from the input string. To remove the specific character we need to use the “-d” option with the tr command.
Command:
cat file.txt
cat file.txt | tr -d 'W'
Explanation:
In the above tr command, we can remove the specific character from input data. We are using the “-d” option with character or string needs to be removed.
Output:
Conclusion
We have seen the uncut concept of “Linux tr Command” with the proper example, explanation, and command with different outputs. The tr command is useful to translate or delete the character from the input string. It will use data|text manipulation and transformation jobs.
Recommended Article
We hope that this EDUCBA information on “Linux tr Command” was beneficial to you. You can view EDUCBA’s recommended articles for more information.