Home·Terminal commands

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

FlagMeaning
-dDelete every character in the set
-sSqueeze repeats of a character into one
-cUse the complement of the set
-cdDelete everything that is not in the set

Examples

tr 'a-z' 'A-Z' < notes.txt

Uppercases everything.

tr -d '\r' < win.txt > unix.txt

Strips CR, turning CRLF endings into LF.

tr -s ' ' < spaced.txt

Collapses 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