When processing massive text files or logs in Linux, you rarely need every single character. You might only need to extract a specific column of data, or perhaps you need to convert a messy block of text into a standardized format before piping it into another command.
The tr (translate) command is a highly specialized Unix utility designed for exactly this purpose. It reads from standard input, identifies specific characters, and either translates them into different characters or deletes them entirely.
In this guide, you will learn how to use the tr command to manipulate text streams efficiently.
The Basic Translate Function
The most common use of tr is to swap one character for another. The syntax requires two sets of characters. tr will look for characters in SET1 and replace them with the corresponding characters in SET2.
tr 'SET1' 'SET2'
Because tr only works with standard input (it cannot read a file directly), you must use cat or input redirection (<) to feed data into it.
Example 1: Converting Lowercase to Uppercase
If you have a file named data.txt containing lowercase text and you want to capitalize everything:
cat data.txt | tr 'a-z' 'A-Z'
The command maps the entire lowercase alphabet (SET1) to the entire uppercase alphabet (SET2).
Example 2: Replacing Delimiters
If you have a CSV file where the columns are separated by colons (like /etc/passwd) and you want to change them to commas:
cat /etc/passwd | tr ':' ','
The Delete Function (The -d Flag)
The true power of tr is its ability to surgically remove specific characters using the -d (delete) flag. When using this flag, you only provide one set of characters.
tr -d 'SET1'
Example 1: Removing Carriage Returns
When you transfer a text file from a Windows machine to a Linux server, Windows often leaves invisible carriage return characters (\r) at the end of every line, which can severely break Linux shell scripts. You can instantly sanitize the file:
cat windows_script.sh | tr -d '\r' > linux_script.sh
Example 2: Stripping Punctuation
If you want to remove all commas and periods from a block of text:
cat document.txt | tr -d '.,'
The Squeeze Function (The -s Flag)
The -s (squeeze-repeats) flag is invaluable for cleaning up messy formatting. If a file contains multiple consecutive instances of a character, the -s flag “squeezes” them down to a single instance.
Example: Compressing Whitespace
If a log file separates data with erratic, unpredictable spaces (e.g., User Login Success), it is very difficult to process with tools like cut or awk. You can squeeze all consecutive spaces down to exactly one space:
cat messy_log.txt | tr -s ' '
The output becomes: User Login Success.
By mastering the translate, delete, and squeeze functions of the tr command, you can quickly sanitize raw text streams, ensuring your data is perfectly formatted before it hits your main processing scripts.