tr

translation filter 

Command


SYNOPSIS

tr [-cs] [-i input_file] [-o output_file] string1 [string2]

tr -s [-c] [-i input_file] [-o output_file] string1 [string2]

tr -d [-c] [-i input_file] [-o output_file] string1

tr -ds [-c] [-i input_file] [-o output_file] string1 string2


DESCRIPTION

tr copies data read from the standard input to the standard output (unless the -i or -o options are specified), substituting or deleting characters as specified by the options, string1 and string2. string1 and string2 are considered to be sets of characters. In its simplest form, tr translates each character in string1 into the character at the corresponding position in string2.

Options

-c 

complements the set of characters specified by string1. This means that tr constructs a new set of characters, consisting of all the characters not found in string1 and uses this new set in place of string1.

-d 

deletes input characters found in string1 from the output.

-i input_file 

specifies an input file to be read instead of the standard input.

-o output_file 

specifies an output file to be written to instead of the standard output.

-s 

checks for sequences of a string1 character repeated several consecutive times. When this happens, tr replaces the sequence of repeated characters with one occurrence of the corresponding character from string2; if string2 is not specified, the sequence is replaced with one occurrence of the repeated character itself. For example,

tr -s abc xyz

translates the input string aaaabccccb into the output string of xyzy.

If you specify both the -d and -s options, you must specify both string1 and string2. In this case, string1 contains the characters to be deleted while string2 contains characters which are to have multiple consecutive appearances replaced with one appearance of the character itself. For example,

tr -ds a b

translates the input string abbbaaacbb into the output string bcb.

The actions of the -s option take place after all other deletions and translations.

String Elements

You can use the following conventions to represent elements of string1 and string2.

character 

Any character not described by the conventions that follow represents itself.

\ooo 

An octal representation of a character with a specific coded value. It can consist of one, two, or three octal digits.

\character 

The \ (backslash) character is used as an escape to remove the special meaning of character. It also introduces escape sequences for non-printing characters, in the manner of C character constants: \\, \a, \b, \f, \n, \r, \t and \v.

c1-c2 

This represents all characters between characters c1 and c2 (in the current locale's collating sequence) including the end values. For example, a-z represents all the lowercase letters in the POSIX locale while A-Z represents all that locale's uppercase letters. One way to convert lowercase and uppercase is with the following filter:

tr 'a-z' 'A-Z'

This is not, however, the recommended method; use the [:class:] construct instead.

[c*n] 

This represents n repeated occurrences of character c. (If n has a leading zero, tr assumes it is octal; otherwise, it is assumed to be decimal.) You can omit the number for the last character in a subset. This representation is only valid in string2.

[:class:] 

This represents all characters that belong to the character class class in the locale indicated by LC_CTYPE. When the class [:upper] or [:lower:] appears in string1 and the opposite class, [:lower:] or [:upper:] appears in string2, tr uses the LC_CTYPE tolower or toupper mappings in the same relative positions. Classes other than [:upper:] or [:lower:] cannot be used for character mappings in string2, since the order of characters in a class is unspecified.

[=c=] 

This represents all characters which belong to the same equivalence class as the character c in the locale indicated by LC_COLLATE. Equivalence classes cannot be used for character mappings in string2. Only internationalized versions of tr support this format.


EXAMPLES

tr -cs "[:alpha:]" "[\n*]" <file1 >file2

creates a list of all words (strings of letters) found in file1 and puts it in file2. This is accomplished by replacing strings of one or more non-alphabetic characters with a single newline.


DIAGNOSTICS

Possible exit status values are:

0 

Successful completion.

1 

Failure because of unknown command line option, or too few arguments.


PORTABILITY

POSIX.2. x/OPEN Portability Guide 4.0. Windows 8.1. Windows Server 2012 R2. Windows 10. Windows Server 2016. Windows Server 2019. Windows 11. Windows Server 2022.

Backwardly compatible with both the UNIX Version 7 and System V variants of this command, but with extensions (C escapes, handles ASCII NUL, internationalization).


AVAILABILITY

PTC MKS Toolkit for Power Users
PTC MKS Toolkit for System Administrators
PTC MKS Toolkit for Developers
PTC MKS Toolkit for Interoperability
PTC MKS Toolkit for Professional Developers
PTC MKS Toolkit for Professional Developers 64-Bit Edition
PTC MKS Toolkit for Enterprise Developers
PTC MKS Toolkit for Enterprise Developers 64-Bit Edition


SEE ALSO

Miscellaneous:
regexp


PTC MKS Toolkit 10.4 Documentation Build 39.