Frequently Asked Question

What does tr do, and why is it not a regex tool?

tr (translate) is the simplest of the text-processing utilities: it reads stdin, replaces or deletes individual characters, and writes the result to stdout. tr a-z A-Z upper-cases the input. tr -d '\r' deletes carriage returns. tr -s ' ' squeezes runs of spaces into a single space. tr -c 'a-zA-Z' '\n' translates every non-letter into a newline, which is one way to get a list of words from a file.

tr does not understand regular expressions. Its arguments are character sets, not patterns, and it always works one character at a time. That is the trade-off: it is extremely fast and extremely simple, but it cannot match sequences, cannot anchor to line positions, and cannot do conditional replacement. For anything more complicated than character-by-character translation, reach for sed or awk. For the simple jobs tr is designed for, it is the most concise tool on the system.

Further reading and video