| tr | |
|---|---|
The tr command | |
| Original authors | Douglas McIlroy (AT&T Bell Laboratories) |
| Developers | Variousopen-source andcommercial developers |
| Initial release | November 1973; 52 years ago (1973-11) |
| Written in | C |
| Operating system | Unix,Unix-like,Plan 9,Inferno,OS-9,MSX-DOS,IBM i |
| Platform | Cross-platform |
| Type | Command |
| License | coreutils:GPLv3+ Plan 9:MIT License |
| Repository | |
tr is acommand inUnix,Plan 9,Inferno, andUnix-like operating systems. It is an abbreviation oftranslate ortransliterate, indicating its operation of replacing or removing specific characters in its input data set.
The utility reads a byte stream from itsstandard input and writes the result to thestandard output. Asarguments, it takes two sets of characters (generally of the same length), and replaces occurrences of the characters in the first set with the corresponding elements from the second set. For example,
tr 'abcd' 'jkmn'
maps all charactersa toj,b tok,c tom, andd ton.
The character set may be abbreviated by using character ranges. The previous example could be written:
tr 'a-d' 'jkmn'
InPOSIX-compliant versions oftr, the set represented by a character range depends on thelocale'scollating order, so it is safer to avoid character ranges in scripts that might be executed in a locale different from that in which they were written. Ranges can often be replaced withPOSIX character sets such as[:alpha:].
Thes flag causestr to compress sequences of identical adjacent characters in its output to a single token. For example,
tr -s '\n'
replaces sequences of one or more newline characters with a single newline.
Thed flag causestr to delete all tokens of the specified set of characters from its input. In this case, only a single character set argument is used. The following command removes carriage return characters.
tr -d '\r'
Thec flag indicates the complement of the first set of characters. The invocation
tr -cd '[:alnum:]'
therefore removes all non-alphanumeric characters.
The original version oftr was written byDouglas McIlroy and was introduced inVersion 4 Unix.[1]
The version oftr bundled inGNUcoreutils was written by Jim Meyering.[2] The command is available as a separate package forMicrosoft Windows as part of theUnxUtils collection ofnativeWin32ports of common GNU Unix-like utilities.[3] It is also available in theOS-9 shell.[4] Atr command is also part ofASCII'sMSX-DOS2 Tools forMSX-DOS version 2.[5] Thetr command has also been ported to theIBM i operating system.[6]
Most versions oftr, including GNUtr and classic Unixtr, operate on single-byte characters and are notUnicode compliant. An exception is theHeirloom Toolchest implementation, which provides basic Unicode support.
Ruby andPerl also have an internaltr operator, which operates analogously.[7][8]Tcl'sstring map command is more general in that it maps strings to strings while tr maps characters to characters.[9]
tr – Shell and Utilities Reference,The Single UNIX Specification, Version 5 fromThe Open Grouptr(1) – FreeBSD General CommandsManualtr(1) – Solaris 11.4 User Commands ReferenceManualtr(1) – Linux UserManual – User Commands from Manned.orgtr(1) – Plan 9 Programmer's Manual, Volume 1tr(1) – Inferno General commandsManual