Tr Command - Text Replacement Utility | Online Free DevTools by Hexmos

Learn how to use the tr command for text replacement and manipulation. Convert characters, delete, complement, and squeeze text with this powerful Unix utility.

Tr Command

Understanding the Tr Command

The tr command in Unix-like operating systems is a powerful command-line utility used for translating or deleting characters. It reads from standard input and writes to standard output, making it highly versatile for text manipulation tasks. This tool is essential for scripting and data processing when you need to modify character sets within text streams.

Common Tr Command Use Cases

Here are several practical examples demonstrating the capabilities of the tr command:

Replacing Characters with Newlines

One of the most frequent uses of tr is to replace specific characters with newline characters, effectively converting a single line of text into multiple lines. This is particularly useful for processing environment variables like $PATH.

# To replace : with a new line:
echo $PATH | tr ":" "\n"
echo $PATH | tr -t ":" "\n" 

Deleting Specific Characters

The -d option allows you to delete all occurrences of specified characters from the input. This is useful for cleaning up data by removing unwanted characters.

# To remove all occurrences of "ab":
echo aabbcc | tr -d "ab"         # output: cc

Complementing Character Sets

The -c option complements the specified character set. This means that characters *not* in the set are translated, while characters *in* the set are deleted (or translated if a second set is provided).

# To complement "aa":
# ("Complement" means to keep "aa", and replace all others with "x")
echo aabbccd | tr -c "aa" x       # output: aaxxxxx (no newline)

# To complement "ab\n":
echo aabbccd | tr -c "ab\n" x    # output: aabbxxx (with newline)

Squeezing Repeated Characters

The -s option squeezes repeated characters. When used with translation, it squeezes repeated characters from the *output*. When used with deletion or complementing, it squeezes repeated characters from the *input* before processing.

# To preserve all alphabetic characters and replace non-alphabetic characters with a newline, squeezing repeated newlines:
echo $PATH | tr -cs "[:alpha:]" "\n" 

# To convert an ordered list to an unordered list, using '+' as a separator:
echo "1. /usr/bin\n2. /bin" | tr -cs " /[:alpha:]\n" "+"

Handling Null Characters

The tr command can also be used to remove null characters, which is often necessary when dealing with binary files or data streams that might contain them.

# To remove all NULLs:
tr < file-with-nulls -d '\000' > file-without-nulls

Further Resources