Name

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

Synopsis

tr [options] charset1 [charset2]

The tr command performs some simple, useful translations of one set of characters into another. For example, to capitalize everything in 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 Linux text utilities like grep:

$ tr -d '
' < 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, see the -t option.)

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 ([:alnum:], [:digit:], etc.) accepted by grep.

tr also understands the escape characters “a” (^G = ring bell), “” (^H = backspace), “f” (^L = formfeed), “ ” (^J = newline), “ ” (^M = return), “ ” (^I = tab), and “v” (^K = vertical tab) accepted by printf (see Screen Output), as well as the notation nnn to mean the character with octal value nnn.

tr is great for quick and simple translations, but for more powerful jobs consider sed, awk, or perl.

Useful options

-d

Delete the characters in charset1 from the input.

-s

Eliminate adjacent duplicates (found in charset1) from the input. For example, tr -s aeiouAEIOU would squeeze adjacent, duplicate vowels to be single vowels (reeeeeeally would become really).

-c

Operate on all characters not found in charset1.

-t

If charset1 is longer than charset2, make them the same length by truncating charset1. If -t is not present, the last character of charset2 is (invisibly) repeated until charset2 is the same length as charset1.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.131.178