Name

tr — stdin  stdout  - file  -- opt  --help  --version

Synopsis

tr [options] charset1 [charset2]

The tr command (short for “translate”) performs some simple, useful translations of one set of characters into another. For example, to capitalize the text of a file:

cat myfile
This is a very wonderful file.
➜ cat myfile | tr 'a-z' 'A-Z'
THIS IS A VERY WONDERFUL FILE.

or to change all vowels into asterisks:

cat myfile | tr aeiouAEIOU '*'
Th*s *s * v*ry w*nd*rf*l f*l*.

or to delete all vowels:

cat myfile | tr -d aeiouAEIOU
Ths s  vry wndrfl fl.

As a very practical example, delete all carriage returns from a DOS text file so it’s more compatible with Terminal text utilities like grep:

tr -d '\r' < dosfile > newfile

tr translates the first character in charset1 into the first character in charset2, the second into the second, the third into the third, etc. If the length of charset1 is N, only the first N characters in charset2 are used. If charset1 is longer than charset2, the final character in charset2 will be used repeatedly.

Character sets can have the following forms:

Form

Meaning

ABCD

The sequence of characters A, B, C, D.

A-B

The range of characters from A to B.

[x*y]

y repetitions of the character x.

[: class :]

The same character classes accepted by grep, such as [:alnum:], [:digit:], etc.

tr also understands the escape characters “\a” (^G = ring bell), “\b” (^H = backspace), “\f” (^L = formfeed), “\n” (^J = newline), “\r” (^M = return), “\t” (^I = Tab), and “\v” (^K = vertical Tab) accepted ...

Get Macintosh Terminal Pocket Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.