Linux Command Reference ยท Text Processing
Linux tr command
Reference for the Linux tr command: translate, delete and squeeze characters, character classes like [:upper:], complements, and why tr works on bytes.
tr reads standard input and rewrites it one character at a time. It can translate characters from one set to another (upper to lower case, spaces to newlines), delete characters, or squeeze runs of a repeated character into one.
tr takes no file arguments: it always reads standard input and writes standard output, so use a redirect or a pipe. It is not a substitute for sed, because it cannot match words or patterns, only individual characters.
tr [OPTION]... SET1 [SET2]
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Syntax
tr [-cCds] SET1 [SET2] tr -d [-c] SET1 tr -s [-c] SET1
| Option / form | Description |
|---|---|
tr SET1 SET2 |
Translate each character of SET1 into the character at the same position in SET2. |
-d |
Delete characters in SET1 (no SET2). |
-s |
Squeeze each run of a repeated character from the last given set into one. |
-c, -C |
Use the complement of SET1: everything not in it. |
-t |
Truncate SET1 to the length of SET2 instead of padding SET2. |
Set notation:
| Notation | Meaning |
|---|---|
a-z |
A range, in character-code order. |
[:alpha:], [:digit:], [:alnum:], [:upper:], [:lower:], [:space:], [:punct:], [:blank:] |
Character classes. |
\n, \t, \r, \\, \NNN |
Escapes; \NNN is an octal byte. |
[c*n] |
Repeat c n times (in SET2); [c*] repeats to fill the length. |
Returns: 0 on success, non-zero on invalid sets or I/O errors.
Usage notes
- No file operands.
tr a b fileis an error: writetr a b < file. - Sets are lists of characters, not strings.
tr 'cat' 'dog'turns everycintod, everyaintooand everytintog; it does not replace the word. Usesedfor words. - Short
SET2: GNUtrpadsSET2with its last character (tr abcdef xymapsctoy). Use-tto truncateSET1instead. Other implementations behave differently, so avoid relying on it. - Delete needs one set,
-dwithSET2is an error unless combined with-s. -cwith newline:tr -cd '0-9'also deletes the final newline. Add\nto the set ('0-9\n') to keep line structure.- Ranges depend on the locale in some implementations;
[:lower:]and[:upper:]are the portable choice. GNUtrworks on bytes, so multi-byte characters such as accented letters are not translated reliably. Usesed,awkorperlfor Unicode. - Case conversion with classes requires matching sizes:
[:lower:]to[:upper:]works, but[:alpha:]to[:upper:]does not. - Quote the sets so the shell does not expand
[a-z]as a glob or treat\nspecially. - Speed:
tris very fast and uses no memory proportional to input, so it is a good first step in long pipelines.
Related
sed,cut,fold,rev- Lessons: Text pipelines, Regular expressions