tr(1) tr(1)
tr - translate characters
tr [-cds] [string1 [string2<b>]]
tr [-cs] string1 string2
tr -s[-c] string1
tr -d[-c] string1
tr -ds[-c] string1 string2
tr copies the standard input to the standard output with substitution or
deletion of selected characters. Input characters found in string1 are
mapped into the corresponding characters of string2. tr processes
supplementary code set characters according to the locale specified in
the LC_CTYPE environment variable [see LANG on environ(5)]. Searches and
translations are performed on characters, not bytes.
The following options are supported:
-c Complements the set of characters in string1 with respect to the
universe of characters whose codes are 001 through 377 octal.
-d Deletes all input characters in string1.
-s Squeezes all strings of repeated output characters that are in
string2 to single characters.
The following abbreviation conventions may be used to introduce ranges of
characters or repeated characters into the strings:
[a-z] Stands for the string of characters whose ASCII codes run from
character a to character z, inclusive.
[a*n<b>] Stands for n repetitions of a. If the first digit of n is 0, n
is considered octal; otherwise, n is taken to be decimal. A zero
or missing n is taken to be huge; this facility is useful for
padding string2.
The escape character \ may be used as in the shell to remove special
meaning from any character in a string. In addition, \ followed by 1, 2,
or 3 octal digits stands for the character whose code is given by those
digits. When octal notation with the backslash (\) escape character is
used, a backslash is placed before each byte of multibyte characters.
The operands string1 and string2 (if specified) define two arrays of
characters. The constructs in the following list can be used to specify
characters or single-character collating elements. If any of the
constructs result in multi-character collating elements, tr will exclude,
without a diagnostic, those multi-character elements from the resulting
array.
Page 1
tr(1) tr(1)
character
Any character not described by one of the conventions below
represents itself.
\octal Octal sequences can be used to represent characters with specific
coded values. An octal sequence consists of a backslash followed
by the longest sequence of one-, two- or three-octal-digit
characters (01234567). The sequence causes the character whose
encoding is represented by the one-, two- or three-digit octal
integer to be placed into the array. If the size of a byte on the
system is greater than nine bits, the valid escape sequence used
to represent a byte is implementation-dependent. Multi-byte
characters require multiple, concatenated escape sequences of
this type, including the leading \ for each byte.
\character
The backslash-escape sequences (\, \a, \b, \f, \n, \r, \t, \v)
are supported. The results of using any other character, other
than an octal digit, following the backslash are unspecified.
c-c Represents the range of collating elements between the range
endpoints, inclusive, as defined by the current setting of the
LC_COLLATE locale category. The starting endpoint must precede
the second endpoint in the current collation order. The
characters or collating elements in the range are placed in the
array in ascending collation sequence.
[:class:]
Represents all characters belonging to the defined character
class, as defined by the current setting of the LC_CTYPE locale
category. The following character class names will be accepted
when specified in string1: alnum, alpha, blank, cntrl, digit,
graph, lower, print, punct, space, upper, xdigit
In addition, character class expressions of the form [:name:] are
recognised in those locales where the name keyword has been given
a charclass definition in the LC_CTYPE category.
When both the -d and -s options are specified, any of the
character class names will be accepted in string2. Otherwise,
only character class names lower or upper are valid in string2
and then only if the corresponding character class (upper and
lower, respectively) is specified in the same relative position
in string1. Such a specification is interpreted as a request for
case conversion. When [:lower:] appears in string1 and [:upper:]
appears in string2, the arrays will contain the characters from
the toupper mapping in the LC_CTYPE category of the current
locale. When [:upper:] appears in string1 and [:lower:] appears
in string2, the arrays will contain the characters from the
tolower mapping in the LC_CTYPE category of the current locale.
The first character from each mapping pair will be in the array
for string1 and the second character from each mapping pair will
Page 2
tr(1) tr(1)
be in the array for string2 in the same relative position.
Except for case conversion, the characters specified by a
character class expression are placed in the array in an
unspecified order.
If the name specified for class does not define a valid character
class in the current locale, the behaviour is undefined.
[=equiv=]
Represents all characters or collating elements belonging to the
same equivalence class as equiv, as defined by the current
setting of the LC_COLLATE locale category. An equivalence class
expression is allowed only in string1, or in string2 when it is
being used by the combined -d and -s] options. The characters
belonging to the equivalence class are placed in the array in an
unspecified order.
[x*n] Represents n repeated occurrences of the character x. Because
this expression is used to map multiple characters to one, it is
only valid when it occurs in string2. If n is omitted or is zero,
it is interpreted as large enough to extend the string2-based
sequence to the length of the string1-based sequence. If n has a
leading zero, it is interpreted as an octal value. Otherwise, it
is interpreted as a decimal value.
When the -d option is not specified:
Each input character found in the array specified by string1 is replaced
by the character in the same relative position in the array specified by
string2. When the array specified by string2 is shorter that the one
specified by string1, the results are unspecified.
If the -c option is specified, the complements of the characters
specified by string1 (the set of all characters in the current character
set, as defined by the current setting of LC_CTYPE, except for those
actually specified in the string1 operand) are placed in the array in
ascending collation sequence, as defined by the current setting of
LC_COLLATE.
Because the order in which characters specified by character class
expressions or equivalence class expressions is undefined, such
expressions should only be used if the intent is to map several
characters into one. An exception is case conversion, as described
previously.
When the -d option is specified:
Input characters found in the array specified by string1 will be deleted.
Page 3
tr(1) tr(1)
When the -c option is specified with -d , all characters except those
specified by string1 will be deleted. The contents of string2 will be
ignored, unless the -s option is also specified.
The same string cannot be used for both the -d and the -s option; when
both options are specified, both string1 (used for deletion) and string2
(used for squeezing) are required.
When the -s option is specified, after any deletions or translations have
taken place, repeated sequences of the same character will be replaced by
one occurrence of the same character, if the character is found in the
array specified by the last operand. If the last operand contains a
character class, such as the following example:
tr -s '[:space:]'
the last operand's array will contain all of the characters in that
character class. However, in a case conversion, as described previously,
such as:
tr -s '[:upper:]' '[:lower:]'
the last operand's array will contain only those characters defined as
the second characters in each of the toupper or tolower character pairs,
as appropriate.
An empty string used for string1 or string2 produces undefined results.
The following example creates a list of all the words in file1 one per
line in file2, where a word is taken to be a maximal string of
alphabetics. The strings are quoted to protect the special characters
from interpretation by the shell; 012 is the ASCII code for newline.
tr -cs "[A-Z][a-z]" "[\012*]" < file1 > file2
The following example creates a list of all words in file1 one per line
in file2, where a word is taken to be a maximal string of letters.
tr -cs "[:alpha:]" "[\n*]" < file1 <b>> file2
The next example translates all lower-case characters in file1 to uppercase
and writes the results to standard output.
tr "[:lower:]" "[:upper:]" < file1
Note that the caveat expressed in the corresponding Issue 3 example is no
longer in effect. This case conversion is now a special case that employs
the tolower and toupper classifications, ensuring that proper mapping is
accomplished (when the locale is correctly defined).
Page 4
tr(1) tr(1)
This example uses an equivalence class to identify accented variants of
the base character e in file1, which are stripped of diacritical marks
and written to file2.
tr "[=e=]" e < file1 <b>> file2
The following exit values are returned:
0 All input was processed successfully.
>0 An error occurred.
If necessary, string1 and string2 can be quoted to avoid pattern matching
by the shell.
If an ordinary digit (representing itself) is to follow an octal
sequence, the octal sequence must use the full three digits to avoid
ambiguity.
When string2 is shorter than string1, a difference results between
historical System V and BSD systems. A BSD system will pad string2 with
the last character found in string2. Thus, it is possible to do the
following:
tr 0123456789 d
which would translate all digits to the letter d. Since this area is
specifically unspecified in the document, both the BSD and System V
behaviours are allowed, but a portable application cannot rely on the BSD
behaviour. It would have to code the example in the following way:
tr 0123456789 '[d*]'
It should be noted that, despite similarities in appearance, the string
operands used by tr are not regular expressions.
NUL characters can be stripped by using tr -d '\000'.
/usr/lib/locale/locale<b>/LC_MESSAGES/uxcore.abi
language-specific message file [See LANG on environ(5).]
ascii(5), ed(1), sh(1)
tr will not handle ASCII NUL in string1 or string2; it always deletes NUL
from input.
PPPPaaaaggggeeee 5555 [ Back ]
|