TR(1V)

NAME
tr - translate characters

SYNOPSIS
tr [-dcs0] [ string1 string2 ]
tr -d[cs0] string1

DESCRIPTION
Tr copies its standard input to standard output changing or deleting characters as it goes. Options exist to map a sequence of characters into another sequence of characters or to delete a list of characters. It is also possible to compress repeated sequences of a single character to one character.

Without the -d option tr converts every input character which is a member of string1 into the corresponding member of string2. When -d is specified the characters in string1 are deleted instead. When -s is specified multiple adjacent occurences of a character from string2 in the output are compressed into a single character.

By default ASCII NUL characters are removed from the input, however if a NUL character is specified in one of the strings or if the -0 flag is specified this is not done.

The two strings support a limited number of abbreviations to specify ranges of characters or repeated characters. These are handled as follows:

[a-b]
    Replaced by each of the characters in the range a to b (inclusive) in that order.

[a*n]
    Replaced by n occurences of the character a. n is interpreted as an octal number if it starts with a 0 otherwise as a decimal number. If n evaluates to 0 (including the case where n is missing completely) the sequence is replaced by an infinite string of a characters. This effectively terminates the string.

\octal
    A \ character followed by one, two or three octal digits is converted to the character with that octal value. This may be used to insert control characters, including the ASCII NUL character, into the string.

\char
    A \ character followed by any other character is replaced by that character. This can be used to put [ characters into either string.

The contents of string1 may also be removed from the set of characters to consider - see the -c option below.

OPTIONS

-c  string1 is replaced by a string of the characters \001 to \377 (in that order) from which the characters in the original string1 have been removed.

-d  The characters of string1 are deleted from the input. Notice that this happens before the compression of output characters as a result of the -s option.

-s  All strings of a single character from string2 which occur in the output are replaced by one character.

-0  The ASCII NUL characters are not stripped from the input. This is the default if either string1 or string2 contain a \0 escape sequence.

EXAMPLES

To convert lower to upper case:

    tr '[a-z]' '[A-Z]'

To extract all words from the input and print them one per line on the output (assuming a word is a sequence of alphabetic characters delimited by something else):

    tr -cs '[a-z][A-Z]' '[\012*]'

To delete all control characters from the input (including newline characters):

    tr -d '[\0-\37]\177[\200-\237]'

SEE ALSO
cat(1v), iconv(1v), ascii(7).

NOTES

The removal of NUL characters from the input is retained for backwards compatibility with old versions of the command. For maximum portability applications should not rely on tr being able to do anything with NUL characters.

If string2 is shorter than string1 it is extended with the last character which it generates (which may not be the same as the last character in the string - consider the example [a-z] where the character used will be z rather than ].

Empty ranges in either string are skipped completely. Badly formed ranges or repetitions are assumed (silently) to be sequences of literal characters. None of these behaviours are necessarily portable.