tr
Text processing
Usage
tr '[set1]' '[set2]'Replaces or deletes characters one for one; it reads standard input only, so a file name as an argument fails and you must redirect with < or pipe into it.
Common flags
| Flag | Meaning |
|---|---|
| -d | Delete every character in the set |
| -s | Squeeze repeats of a character into one |
| -c | Use the complement of the set |
| -cd | Delete everything that is not in the set |
Examples
tr 'a-z' 'A-Z' < notes.txtUppercases everything.
tr -d '\r' < win.txt > unix.txtStrips CR, turning CRLF endings into LF.
tr -s ' ' < spaced.txtCollapses runs of spaces into one.
These were built to be piped together. Three joined commands are usually shorter than one that does everything.
How to read this
- Square brackets [ ] mark a part you may leave out.
- An ellipsis … means you can list more than one.
- Flags are case-sensitive — in some commands -r and -R do different things.
Questions
Q. What does tr do?
Replaces or deletes characters one for one; it reads standard input only, so a file name as an argument fails and you must redirect with < or pipe into it.
Q. How do I type it?
tr '[set1]' '[set2]' — square brackets mark the parts you can leave out.
Q. How many flags are worth knowing?
4 are listed here; the full set is in man tr. This command sits under Text processing.
Related commands
man page: man tr