[Next] [Previous] [Up] [Top] [Contents]

8.1 Working With Files

8.1.13 tr - translate characters

The tr command translates characters from stdin to stdout.

Syntax

tr [options] string1 [string2]

With no options the characters in string1 are translated into the characters in string2, character by character in the string arrays. The first character in string1 is translated into the first character in string2, etc.

A range of characters in a string is specified with a hyphen between the upper and lower characters of the range, e.g. to specify all lower case alphabetic characters use '[a-z]'.

Repeated characters in string2 can be represented with the '[x*n]' notation, where character x is repeated n times. If n is 0 or absent it is assumed to be as large as needed to match string1.

Characters can include \octal (BSD and SVR4) and \character (SVR4 only) notation. Here "octal" is replaced by the one, two, or three octal integer sequence encoding the ASCII character and "character" can be one of:

b back space

f form feed

n new line

r carriage return

t tab

v vertical tab

The SVR4 version of tr allows the operand ":class:" in the string field where class can take on character classification values, including:

alpha alphabetic characters

lower lower case alphabetic characters

upper upper case alphabetic characters

Common Options

-c complement the character set in string1

-d delete the characters in string1

-s squeeze a string of repeated characters in string1 to a single character

Examples

The following examples will use as input the file, a list of P. G. Wodehouse Jeeves & Wooster books.

The Inimitable Jeeves [1923] The Mating Season [1949]

Carry On, Jeeves [1925] Ring for Jeeves [1953]

Very Good, Jeeves [1930] Jeeves and the Feudal Spirit [1954]

Thank You, Jeeves [1934] Jeeves in the Offing [1960]

Right Ho, Jeeves [1934] Stiff Upper Lip, Jeeves [1963]

The Code of the Woosters [1938] Much Obliged, Jeeves [1971]

Joy in the Morning [1946] Aunts Aren't Gentlemen [1974]

To translate all lower case alphabetic characters to upper case we could use either of:

tr '[a-z]' '[A-Z]' or tr '[:lower:]' '[:upper:]'

Since tr reads from stdin we first cat the file and pipe the output to tr, as in:

% cat wodehouse | tr '[a-z]' '[A-Z]'

THE INIMITABLE JEEVES [1923] THE MATING SEASON [1949]

CARRY ON, JEEVES [1925] RING FOR JEEVES [1953]

VERY GOOD, JEEVES [1930] JEEVES AND THE FEUDAL SPIRIT [1954]

THANK YOU, JEEVES [1934] JEEVES IN THE OFFING [1960]

RIGHT HO, JEEVES [1934] STIFF UPPER LIP, JEEVES [1963]

THE CODE OF THE WOOSTERS [1938] MUCH OBLIGED, JEEVES [1971]

JOY IN THE MORNING [1946] AUNTS AREN'T GENTLEMEN [1974]

We could delete all numbers with:

% cat wodehouse | tr -d '[0-9]'

The Inimitable Jeeves [] The Mating Season []

Carry On, Jeeves [] Ring for Jeeves []

Very Good, Jeeves [] Jeeves and the Feudal Spirit []

Thank You, Jeeves [] Jeeves in the Offing []

Right Ho, Jeeves [] Stiff Upper Lip, Jeeves []

The Code of the Woosters [] Much Obliged, Jeeves []

Joy in the Morning [] Aunts Aren't Gentlemen []

To squeeze all multiple occurrences of the characters e, r, and f:

% cat wodehouse | tr -s 'erf'

The Inimitable Jeves [1923] The Mating Season [1949]

Cary On, Jeves [1925] Ring for Jeves [1953]

Very Good, Jeves [1930] Jeves and the Feudal Spirit [1954]

Thank You, Jeves [1934] Jeves in the Ofing [1960]

Right Ho, Jeves [1934] Stif Upper Lip, Jeves [1963]

The Code of the Woosters [1938] Much Obliged, Jeves [1971]

Joy in the Morning [1946] Aunts Aren't Gentlemen [1974]


Introduction to Unix - 14 AUG 1996
[Next] [Previous] [Up] [Top] [Contents]