Data imported into a Linux server from external sources is rarely perfectly formatted. You might receive a text file where a rogue script has inserted carriage return characters (^M) at the end of every line, or a database export where you need to completely remove all punctuation marks before passing the text into an indexing engine. While you could open the file in a text editor like nano or vim and run a Find-and-Replace command, doing so manually is highly inefficient and impossible to automate within a bash script. The Linux tr (translate) command is a specialized, lightweight utility designed precisely to translate, squeeze, or delete specific characters from standard input streams.
Understanding How tr Operates
Unlike sed or awk, which can process entire words or complex regular expression patterns, tr operates strictly on individual characters. Furthermore, tr cannot read files directly; it only reads from standard input. Therefore, you must always pipe (|) data into it or use input redirection (<).
The basic syntax requires two character sets: Set 1 (the characters to find) and Set 2 (the characters to replace them with).
cat file.txt | tr 'SET1' 'SET2'
Translating Characters
The most common use of tr is to swap one specific character for another.
- Converting colons to commas: If you are converting a colon-delimited file (like
/etc/passwd) into a standard CSV file, you map the colon (Set 1) to a comma (Set 2).cat /etc/passwd | tr ':' ',' - Converting lowercase to UPPERCASE: The
trcommand understands ranges. You do not need to type out the entire alphabet. You can simply specify the lowercase range[a-z]and map it to the uppercase range[A-Z].cat lower.txt | tr 'a-z' 'A-Z'
Deleting Characters
If you want to completely eradicate a character from a file rather than replacing it, you use the -d (delete) flag. When using -d, you only provide a single character set (Set 1).
- Removing all numbers from a string: To strip all numerical digits (0 through 9) from a text file, leaving only the alphabetical characters and punctuation, run:
cat mixed_data.txt | tr -d '0-9' - Removing newline characters: If you have a file containing a list of items on separate lines, and you want to crush them all onto a single, continuous line of text, you can delete the hidden newline character (
\n):cat list.txt | tr -d '\n'
Squeezing Repeating Characters
A highly useful feature of tr is the “squeeze repeats” flag (-s). This flag scans the text for consecutive instances of the same character and collapses them down into a single instance.
For example, if you have a log file that is poorly formatted with multiple, erratic spaces between words (e.g., Error Code 500), you can squeeze the spaces:
cat bad_log.txt | tr -s ' '
The output will replace the massive gaps with a single, clean space (Error Code 500), making the text much easier to parse with other command-line tools like cut.