tr command in Linux
The Linux tr command is a versatile utility for translating or deleting characters. It can be used to perform various text manipulation tasks, such as converting lowercase letters to uppercase, replacing specific characters, and deleting unwanted characters. The tr command is often used in conjunction with other commands in shell scripts to automate data processing tasks efficiently. It is a handy tool for anyone working with text data on the Linux command line.
tr Syntax:
Linux tr Options:
Option | Description |
---|---|
-c | Complement the set |
-d | Delete characters in SET1 |
-s | Squeeze multiple occurrences |
-t | Translate using SET2 |
-u | Transform only characters in SET1 |
-C | Complement the range |
-d | Delete characters in SET1 |
-s | Squeeze multiple occurrences |
-t | Translate using SET2 |
-u | Transform only characters in SET1 |
tr Parameters:
Parameter | Description |
---|---|
SET1 | Specify the characters to replace or delete |
SET2 | Specify the characters to replace with |
How to use tr command:
Convert lowercase letter to uppercase
This command converts all lowercase letters in the input string to uppercase.
Remove specific characters from a string
This tr command removes all digits from the input string.
Translate spaces to tabs in a file
This command translates all spaces in the input file to tabs and writes the output to a new file.
Replace specific characters in a string
This tr command replaces ‘e’ with ‘1’ and ‘l’ with ‘2’ in the input string.
Count the number of words in a file
This command counts the number of words in a file by converting spaces to newlines and then using wc to count the lines.
Delete specific characters from a string
This tr command deletes all alphabetic characters from the input string.
Remove duplicate characters in a string
This command squeezes multiple occurrences of alphabetic characters in the input string to a single occurrence.
Convert tabs to spaces in a file
This tr command converts all tabs in the input file to spaces and writes the output to a new file.
How do I use tr in Linux?
To use the tr command in Linux, execute the following command:
How can I translate characters using tr in bash?
You can translate characters using the tr command in bash by specifying the characters to be replaced and the characters to replace them with.
How do I delete characters using tr in Linux?
To delete characters using the tr command in Linux, simply omit the characters to replace with.
How can I convert lowercase to uppercase using tr in bash?
To convert lowercase to uppercase using tr in bash, specify the ranges of characters to be translated.
How do I squeeze repeated characters using tr in Linux?
To squeeze repeated characters using the tr command in Linux, use the -s
option.
How can I use tr to complement a set of characters in bash?
You can complement a set of characters using the tr command in bash by using the -c
option.
How do I truncate a set of characters using tr in Linux?
To truncate a set of characters using the tr command in Linux, only list the characters to keep in the translation set.
How can I squeeze consecutive occurrences of a set of characters using tr in bash?
You can squeeze consecutive occurrences of a set of characters using the tr command in bash by combining the -s
option with the characters to squeeze.
Applications of the tr command
- Translate characters
- Delete characters
- Squeeze repeated characters
- Convert uppercase to lowercase
- Convert lowercase to uppercase