tr
Character-by-character replacement and deletion — case, newlines, extra spaces
tr (translate) works with individual characters, not with strings or regexes: it replaces characters from one set with the corresponding characters from another, deletes them or squeezes repeats. It reads only standard input and writes to standard output.
Syntax
tr [options] 'set1' 'set2' < file # replace
tr -d [options] 'set1' < file # delete
tr -s 'set1' < file # squeeze repeats
When replacing, the first character of set1 becomes the first character of set2, the second becomes the second, and so on.
Options
| Option | What it does |
|---|---|
-d | delete the characters in set1 |
-s | squeeze consecutive identical characters from the last given set into one; done after replacing or deleting |
-c (-C) | use the complement of set1 — all characters except those listed |
-t | when replacing, truncate set1 to the length of set2 (instead of extending set2) |
How to write sets
| Syntax | Meaning |
|---|---|
abc | the characters themselves |
a-z, 0-9 | a range in code order |
[:lower:] [:upper:] | lowercase / uppercase letters; as a pair they change case |
[:digit:] [:xdigit:] | digits / hex digits |
[:alpha:] [:alnum:] | letters / letters and digits |
[:space:] / [:blank:] | all whitespace, including \n / only space and tab |
[:punct:] [:cntrl:] [:print:] [:graph:] | punctuation / control / printable with space / printable without space |
\n \t \r \\ | newline, tab, carriage return, backslash |
\NNN | a character with an octal code, e.g. \000 — a null byte |
[x*] / [x*5] | in the second set: x as many times as needed to match the length of the first / 5 times |
Examples
# to uppercase
echo 'hello' | tr 'a-z' 'A-Z'
echo 'hello' | tr '[:lower:]' '[:upper:]'
# squeeze repeated spaces into one
echo 'a b c' | tr -s ' '
# delete all digits / keep only digits
echo 'a1b2c3' | tr -d '0-9'
echo 'a1b2c3' | tr -cd '0-9'
# separator to newline — each value on its own line
echo 'a:b:c' | tr ':' '\n'
echo "$PATH" | tr ':' '\n'
# join lines into one, separated by spaces
printf 'one\ntwo\nthree\n' | tr '\n' ' '
# remove Windows \r at line ends
printf 'line\r\n' | tr -d '\r' | od -c
# replace several characters at once: l → 0, o → 1
echo 'hello' | tr 'lo' '01'
# everything that isn't a letter or digit becomes an underscore, repeats squeezed
echo 'Hello, World! 2024' | tr -cs '[:alnum:]' '_'
# split text into words: each on its own line
echo 'one two three' | tr -s '[:space:]' '\n'
# null bytes to newlines (e.g. /proc/*/environ)
tr '\0' '\n' < /proc/1/environ | head -n 3
# ROT13
echo 'Hello' | tr 'A-Za-z' 'N-ZA-Mn-za-m'
Pitfalls
- This isn't string replacement.
tr 'abc' 'xyz'changesa→x,b→y,c→zindividually, wherever they are. To replace a whole word you need sed. - A short second set is padded with its last character.
echo abc | tr abc xgivesxxx. With-t, the extra characters of the first set are simply left alone:echo abc | tr -t abc xgivesxbc. - A file is passed only via
<.tr a b file— errorextra operand. - Square brackets are ordinary characters.
tr '[a-z]' '[A-Z]'works only because[maps to[. Without quotes, the shell may expand[a-z]if the directory has a one-letter file name. - A hyphen between characters is a range. To replace the hyphen itself, put it last in the set:
tr 'a-' 'x_'. A set that starts with-and a letter ('-x') is taken by tr as an option — writetr -- '-x' '_y'. trdoesn't understand multibyte characters. GNU tr works with bytes, so a range liketr 'à-ÿ' 'À-Þ'mangles accented letters in any locale. For them use sed (s/.*/\U&/) or awk (toupper) in a UTF-8 locale.