Tr command in Linux can translate, compress (squeeze), or remove characters that are supplied from the standard input (STDIN), and it outputs the results to your Linux terminal screen, which is the standard output (STDOUT).
You can carry out helpful operations using the tr command, including character replacement, character deletion, and character conversion from lowercase to uppercase or vice versa. It is typically used via piping in conjunction with other commands.
Using real-world examples and thorough explanations of the most popular choices, this article will show you how to use the tr command in Linux productively.
Define Tr Command in Unix
Tr is a command line tool for deleting or translating characters in a UNIX operating system. Various transformations are supported, squeezing repetitive characters, including changing uppercase to lowercase, eliminating particular characters, and basic search and replace. It can support more complicated translation when used with UNIX pipes.
The tr command in Linux or UNIX duplicates the input and produces the output by replacing or omitting certain characters. Translate or transliterate is abbreviated as tr.
It accepts two sets of characters as parameters and swaps out instances of the characters in the first set with the matching elements in the second set, translating characters. It is frequently employed in shell scripts and other programs.
Functions of the Tr Command
The tr command in Linux replaces or removes characters from standard input, then outputs the result. Depending on the flags supplied and the strings specified by the String1 and String2 variables, the tr command does one of three types of operations.
- The Evolution of Characters
The tr command replaces each character in String1 from the standard input with the character at the same position in String2 if String1 and String2 are supplied, and the -d flag is not identified.
- Character Removal with the -d Flag
The tr command removes all of the characters in String1 from standard input if the -d parameter is given.
- Deleting Sequences using the -s Flag
The tr command in Linux eliminates all characters using an online Pascal editor to save the first one from any sequence of a character string specified in String1 or String2 if the -s flag is given.
- The tr command eliminates all but the first instance of each character in String1 from the standard output.
- The tr command eliminates all except the first instance of each character represented in String2 from a list of instances of the specific character in standard output.
How Does Tr Command in Linux Work?
- Characters Can Be Removed Using the tr Command, tr is most frequently used to remove characters from an input stream. To delete a character, a group of characters, or an interpreted sequence, use the -d (--delete) option.
- i) Delete the letter "r" in lowercase everywhere:
$ echo "Round and round the rugged rocks the ragged rascal ran" | tr -d r Round and ound the ugged ocks the agged ascal an
- ii) In both lowercase and uppercase, delete all occurrences of the letter "r":
$ echo "Round and round the rugged rocks the ragged rascal ran" | tr -d 'Rr'
ound and ound the ugged ocks the agged ascal an
iii) Delete all occurrences of the letters "a-j":
$ echo "Round and round the rugged rocks the ragged rascal ran" | tr -d 'a-j.'
Roun n roun t rugg rok t rgg rsl rn
- iv) You can also eliminate all uppercase letters by using interpreted sequences, for instance:
$ echo "Round and round the rugged rocks the ragged rascal ran" | tr -d [:upper:]
ound and round the rugged rocks, the ragged rascal ran
- Using the tr Command to Replace
You can substitute characters with the tr command. Two sets of characters are used as arguments, and the characters from the first set are swapped out for the characters from the second set.
- i) Change all lowercase "t" letters to uppercase "T" letters:
$ echo "Round and round the rugged rocks the ragged rascal ran" | tr 'r' 'R'
Round and Round the Rugged Rocks the Ragged Rascal Ran
- ii) Ranges are also applicable here
Here, we'll substitute the numbers 1-4 for the letters a to d as an extreme example. Accordingly, an is effectively mapped to 1, b to 2, c to 3, and d to 4. Example:
$ echo "Round and round the rugged rocks the ragged rascal ran" | tr 'a-d' '1-4.'
Roun4 1n4 roun4 the rugge4 ro3ks the r1gge4 r1s31l r1n
iii) Changing All Lowercase for Uppercase Letters
The tr command's most well-known application is to change all lowercase letters to uppercase.
$ echo "Round and round the rugged rocks the ragged rascal ran" | tr [:lower:] [:upper:]
ROUND AND ROUND THE RUGGED ROCKS THE RAGGED RASCAL RAN
- Removing Repeated Characters with the tr Command (Squeeze)
The -s (--squeeze-repeats) option of the tr command is another champion option. Squeeze replaces several instances of a character with a single occurrence of that character. This is frequently used to eliminate extra empty lines or duplicate spacing.
Here is an example of a test.txt file to show this:
$ cat test.txt
Unusual spacing in the first line
Followed by a bunch of blank lines.
The final line then has strange spacing.
The -s argument of the tr command will combine double spaces into one space.
$ cat test.txt | tr -s "\"
Unusual spacing in the first line
Followed by a bunch of blank lines.
The final line then has strange spacing.
Using the complement option of the tr command
Using the complement of SET1 is all that the man page says, which initially perplexed me. However, after careful consideration and additional testing, it made sense.
Consider the entire alphanumeric characters: A-Z, a-z, and 0 to 9. Take the letter "s" right now. The remaining alphabetic characters are the letter "s"'s complement. The "s" would require all the other characters to complete the set.
Via means of the tr Command Truncate Option
Here's another choice that briefly sent me into a tailspin. When you first see the word "truncate," you might think you're using this option to shorten the input string.
It is employed to reduce the length of the first argument to match that of the second. This alters how the translation functions.
Conclusion
Despite not having the most logical options, the tr command in Linux is a helpful tool. Hope you found this article interesting and informational.