tr(1) tr(1)
NAME
tr - transliterate characters
SYNOPSIS
tr[ -c][ -d][ -s][ --] string1[ string2]
DESCRIPTION
tr reads an input text from standard input, replaces or deletes
selected characters from it, and writes the result to standard output.
Character substitution
If you wish to substitute characters using tr, you must define both
strings. The -d option must not be specified.
tr replaces each character in the input text that appears in string1
with the appropriate character from string2: the i-th character in
string1 is replaced in the input text by the i-th character in
string2. If string2 is shorter than string1, the characters from
string1, for which there are no corresponding characters in string2,
are not replaced (see Example 1).
Character deletion
tr deletes characters if the -d option (any character) or the -s
option (only characters that appear a number of times) is specified.
-d considers all characters from string1. -s works with the first or
second string, depending on whether the -d option is specified as
well.
OPTIONS
-c (c - complement) Complements string1 with respect to the
currently applicable character set (octal values 001 through
377). The complemented string1 then then contains all characters
of the currently applicable character set except for those speci-
fied in the original string1.
-c is always executed first by tr. In the input text, tr then
replaces the i-th character in the complemented string1 with the
i-th character in string2, or deletes the characters that appear
in the complemented string.
-d (d - delete) All input characters that appear in string1 are
deleted.
Together with the -c option: All input characters that appear in
the complemented string string1 are deleted.
Together with the -s option: After deletion, each sequence of the
same characters that appear in string2 are truncated to a single
character in the output text. If -s is not specified, string2 is
ignored. If string2 is not specified, -s is ignored.
Page 1 Reliant UNIX 5.44 Printed 11/98
tr(1) tr(1)
-s (s - squeeze) tr truncates each sequence of the same characters
that appear in string1 or string2 to a single character in the
output text (see Example 3). string2 is then only used by -s if
the -d option is specified as well.
string1 [string2]
In string1, specify the characters to be replaced or deleted.
string2 is the replacement string (except where -d and -s are
combined).
The characters in both strings must be specified without inter-
vening blanks or other delimiters.
If a string contains metacharacters that have a special meaning
for the shell, these metacharacters must be escaped by enclosing
the entire string in single quotes '...' or by preceding each
such character with a backslash \.
The strings can contain the following specifications:
character
any printable character.
\octalnumber
whereby octalnumber is a one-, two-, or three-digit octal
number. The backslash must be escaped so that the digits are
recognized as an octal number.
tr also processes the NUL character (\000).
Warning: In previous versions, the NUL character was always
deleted as an input character.
metacharacter
as escape sequences (same as for the printf command). The
following escape sequences can be specified:
\\ Backslash (for distinguishing octal numbers)
\a Warning, alert
\b Backspace
\f Form feed
\n Newline
\r Carriage return
\t Tab
\v Vertical tab
Page 2 Reliant UNIX 5.44 Printed 11/98
tr(1) tr(1)
a-z or [a-z]
Stands for the set of characters from a to z inclusive.
[Characters are sorted in the currently applicable collating
sequence. Unlike in internationalized regular expressions, a
and z must be ordinary characters, i.e. not equivalence
class expressions [=c=] or collating symbols [.cc.].
[:class:]
class specifies a character class, similar to international-
ized regular expressions. The following values are possible
for class:
alnum, alpha, blank, cntrl, digit, graph, lower, print,
punct, space, upper, xdigit.
Character classes must not be specified in a replacement
string. Exception: The classes lower and upper are permitted
if the corresponding character class is specified on the
same side in string1.
[=equivalence=]
equivalence specifies an equivalence class, similar to
internationalized regular expressions.
Equivalence classes must not be specified in a replacement
string.
[a*n]
Stands for n repetitions of the a, e.g. [a*3] stands for
aaa. Only useful in a replacement string.
If the first digit of n is 0, n is considered octal; other-
wise, it is taken to be decimal.
If n is 0 or is omitted, it is taken to be "huge", meaning
that the preceding character is to be repeated as often as
required to pad string2 to the length of string1 (see Exam-
ple 1).
string2 not specified: string1 (possibly complemented, see -c) is
used for string2.
LOCALE
The LCMESSAGES environment variable governs the language in which
message texts are displayed.
In expressions in square brackets, the LCCOLLATE environment variable
governs the scope of character ranges (eg. [a-z]). LCCTYPE also
specifies which characters are included in the currently valid charac-
ter set in conjunction with the -c option.
If LCMESSAGES, LCCOLLATE or LCCTYPE is undefined or is defined as
Page 3 Reliant UNIX 5.44 Printed 11/98
tr(1) tr(1)
the null string, it defaults to the value of LANG. If LANG is likewise
undefined or null, the system acts as if it were not international-
ized.
The LCALL environment variable governs the entire locale. LCALL
takes precedence over all the other environment variables which affect
internationalization.
If any of the locale variables has an invalid value, the system acts
as if none of the variables were set.
EXAMPLES
Example 1
tr without options: simple examples to demonstrate how tr works:
$ cat days
Monday Tuesday Wednesday Thursday Friday Saturday Sunday
$ tr TS ts <days
Monday tuesday Wednesday thursday Friday saturday sunday
Here tr replaces all occurrences of T with t and of S with s.
$ tr TSF ts <days
Monday tuesday Wednesday thursday Friday saturday sunday
The second string has fewer characters than the first in this case. tr
replaces T by t and S by s, but leaves F unaltered.
Now let us try replacing all lowercase letters with x. The following
solution is not suitable:
$ tr '[a-z]' x <days
Mondxy Tuesdxy Wednesdxy Thursdxy Fridxy Sxturdxy Sundxy
tr has clearly only replaced occurrences of a with x. To replace all
lowercase letters, we need to call tr as follows:
$ tr '[a-z]' '[x*]' <days
Mxxxxx Txxxxxx Wxxxxxxxx Txxxxxxx Fxxxxx Sxxxxxxx Sxxxxx
Each character in string1 now has a corresponding x in string2, as the
asterisk (*) causes string2 to be padded with x as often as required.
The single quotes are essential, since the strings include shell meta-
characters.
Example 2
We want to create a list of all the words that appear in textfile, one
word to a line, a word being defined as any consecutive string con-
sisting only of letters.
Page 4 Reliant UNIX 5.44 Printed 11/98
tr(1) tr(1)
$ cat textfile
"When shall we three meet again?
In thunder, lightning, or in rain?"
$ tr -cs '[:alpha:]' '[\n*]' <textfile
When
shall
we
three
meet
again
In
thunder
lightning
or
in
rain
Example 3
Deleting a non-printing character from a file (tr -d):
$ tr -d '\016' <file
tr deletes from the file the character with the octal code 016 and
displays the result on the standard output.
SEE ALSO
ed(1), sed(1), sh(1).
Page 5 Reliant UNIX 5.44 Printed 11/98