SEARCH
NEW RPMS
DIRECTORIES
ABOUT
FAQ
VARIOUS
BLOG

BotDetect - Real-Time Bot Detection API
 
 

MAN page from RedHat Other textutils-1.22-1.1.i386.rpm

TR

Section: User Commands (1)
Updated: GNU Text Utilities
Index 

NAME

tr - translate or delete characters 

SYNOPSIS

tr[-cst] [--complement] [--squeeze-repeats][--truncate-set1] string1 string2
tr{-s,--squeeze-repeats} [-c] [--complement] string1
tr{-d,--delete} [-c] string1
tr{-d,--delete} {-s,--squeeze-repeats} [-c] [--complement]string1 string2

GNUtralso accepts the --help and --version options. 

DESCRIPTION

This documentation is no longer being maintained and may be inaccurateor incomplete. The Texinfo documentation is now the authoritative source.

This manual page documents the GNU version oftr.trcopies the standard input to the standard output,performing one of the following operations:

• translate, and optionally squeeze repeated characters in the result
• squeeze repeated characters
• delete characters
• delete characters, then squeeze repeated characters from the result.

The string1 and (if given) string2 arguments defineordered sets of characters, referred to below as set1 and set2. Thesesets are the characters of the input thattroperates on. The--complement(-c) option replaces set1 with its complement (all of thecharacters that are not in set1). 

SPECIFYING SETS OF CHARACTERS

The format of the string1 and string2 arguments resemblesthe format of regular expressions; however, they are not regularexpressions, only lists of characters. Most characters simplyrepresent themselves in these strings, but the strings can contain theshorthands listed below, for convenience. Some of them can be usedonly in string1 or string2, as noted below.

Backslash escapes. A backslash followed by a character not listedbelow causes an error message.

\a
Control-G.
\b
Control-H.
\f
Control-L.
\n
Control-J.
\r
Control-M.
\t
Control-I.
\v
Control-K.
\ooo
The character with the value given by ooo, which is 1 to 3 octaldigits.
\\
A backslash.

Ranges. The notation `m-n' expands to all of thecharacters from m through n, in ascending order. mshould collate before n; if it doesn't, an error results. As anexample, `0-9' is the same as `0123456789'.Although GNUtrdoes not support the System V syntax that uses square brackets toenclose ranges, translations specified in that format will still work aslong as the brackets in string1 correspond to identical brackets in string2.

Repeated characters. The notation `[c*n]' instring2 expands to n copies of character c. Thus,`[y*6]' is the same as `yyyyyy'. The notation `[c*]' instring2 expands to as many copies of c as are needed tomake set2 as long as set1. If n begins with a 0, it isinterpreted in octal, otherwise in decimal.

Character classes. The notation `[:class-name:]' expands to allof the characters in the (predefined) class named class-name.The characters expand in no particular order, except for the `upper'and `lower' classes, which expand in ascending order.When the--delete(-d) and--squeeze-repeats(-s) options are both given, any character class can be used instring2. Otherwise, only the character classes `lower' and`upper' are accepted in string2, and then only if thecorresponding character class (`upper' and `lower', respectively) isspecified in the same relative position in string1. Doing thisspecifies case conversion. The class names are given below; an errorresults when an invalid class name is given.

alnum
Letters and digits.
alpha
Letters.
blank
Horizontal whitespace.
cntrl
Control characters.
digit
Digits.
graph
Printable characters, not including space.
lower
Lowercase letters.
print
Printable characters, including space.
punct
Punctuation characters.
space
Horizontal or vertical whitespace.
upper
Uppercase letters.
xdigit
Hexadecimal digits.

Equivalence classes. The syntax `[=c=]' expands to all of thecharacters that are equivalent to c, in no particular order.Equivalence classes are a recent invention intended to supportnon-English alphabets. But there seems to be no standard way todefine them or determine their contents. Therefore, they are notfully implemented in GNUtr;each character's equivalence class consists only of that character,which makes this a useless construction currently. 

TRANSLATING

trperforms translation when string1 and string2 are bothgiven and the --delete (-d) option is not given.trtranslates each character of its input that is in set1 to thecorresponding character in set2. Characters not in set1 are passedthrough unchanged. When a character appears more than once in set1and the corresponding characters in set2 are not all the same, onlythe final one is used. For example, these two commands areequivalent:

tr aaa xyztr a z

A common use oftris to convert lowercase characters to uppercase. This can be done inmany ways. Here are three of them:

tr abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZtr a-z A-Ztr '[:lower:]' '[:upper:]'

Whentris performing translation, set1 and set2 should normally have the samelength. If set1 is shorter than set2, the extra characters at the endof set2 are ignored.

On the other hand, making set1 longer than set2 is not portable;POSIX.2 says that the result is undefined. In this situation, the BSDtrpads set2 to the length of set1 by repeating the last character ofset2 as many times as necessary. The System Vtrtruncates set1 to the length of set2.

By default, GNUtrhandles this case like the BSDtrdoes. When the --truncate-set1 (-t) option is given, GNUtrhandles this case like the System Vtrinstead. This option is ignored for operations other thantranslation.

Acting like the System Vtrin this case breaks the relatively common BSD idiom:

tr -cs A-Za-z0-9 '\012'
because it converts only zero bytes (the first element inthe complement of set1), rather than all non-alphanumerics, tonewlines. 

SQUEEZING REPEATS AND DELETING

When given just the --delete (-d) option,trremoves any input characters that arein set1.

When given just the --squeeze-repeats (-s) option,trreplaces each input sequence of a repeated character that is in set1with a single occurrence of that character.

When given both the --delete and the --squeeze-repeats options,trfirst performs any deletions using set1, then squeezes repeats fromany remaining characters using set2.

The --squeeze-repeats option may also be used when translating, inwhich casetrfirst performs translation, then squeezes repeats from any remainingcharacters using set2.

Here are some examples to illustrate various combinations of options:

Remove all zero bytes:

tr -d '\000'

Put all words on lines by themselves. This converts allnon-alphanumeric characters to newlines, then squeezes each string ofrepeated newlines into a single newline:

tr -cs '[a-zA-Z0-9]' '[\n*]'

Convert each sequence of repeated newlines to a single newline:

tr -s '\n'

GNUtralso accepts the following options in any combination with the others.

--help
Print a usage message and exit with a status code indicating success.
--version
Print version information on standard output then exit.
 

WARNING MESSAGES

Setting the environment variable POSIXLY_CORRECT turns off severalwarning and error messages, for strict compliance with POSIX.2. Themessages normally occur in the following circumstances:

1. When the--deleteoption is given but--squeeze-repeatsis not, and string2 is given, GNUtrby default prints a usage message and exits, because string2 wouldnot be used. The POSIX specification says thatstring2 must be ignored in this case. Silently ignoringarguments is a bad idea.

2. When an ambiguous octal escape is given. For example, \400 isactually \40 followed by the digit 0, because the value 400 octaldoes not fit into a single byte.

Note that GNUtrdoes not provide complete BSD or System V compatibility. For example,there is no option to disable interpretation of the POSIX constructs[:alpha:], [=c=], and [c*10]. Also, GNUtrdoes not delete zero bytes automatically, unlike traditional UNIXversions, which provide no way to preserve zero bytes.


 

Index

NAME
SYNOPSIS
DESCRIPTION
SPECIFYING SETS OF CHARACTERS
TRANSLATING
SQUEEZING REPEATS AND DELETING
WARNING MESSAGES

This document was created byman2html,using the manual pages.
 
ICM Bot detect detector