Tr (Unix)
Template:Short description Template:Lowercase title {{#invoke:Infobox|infobox}}Template:Template other{{#invoke:Check for unknown parameters | check | showblankpositional=1 | unknown = Template:Main other | preview = Page using Template:Infobox software with unknown parameter "_VALUE_"|ignoreblank=y | AsOf | author | background | bodystyle | caption | collapsetext | collapsible | developer | discontinued | engine | engines | genre | included with | language | language count | language footnote | latest preview date | latest preview version | latest release date | latest release version | latest_preview_date | latest_preview_version | latest_release_date | latest_release_version | licence | license | logo | logo alt | logo caption | logo upright | logo size | logo title | logo_alt | logo_caption | logo_upright | logo_size | logo_title | middleware | module | name | operating system | operating_system | other_names | platform | programming language | programming_language | released | replaced_by | replaces | repo | screenshot | screenshot alt | screenshot upright | screenshot size | screenshot title | screenshot_alt | screenshot_upright | screenshot_size | screenshot_title | service_name | size | standard | title | ver layout | website | qid }}Template:Main other tr is a command in Unix, Plan 9, Inferno, and Unix-like operating systems. It is an abbreviation of translate or transliterate, indicating its operation of replacing or removing specific characters in its input data set.
OverviewEdit
The utility reads a byte stream from its standard input and writes the result to the standard output. As arguments, 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 characters a to j, b to k, c to m, and d to n.
The character set may be abbreviated by using character ranges. The previous example could be written:
tr 'a-d' 'jkmn'
In POSIX-compliant versions of tr
, the set represented by a character range depends on the locale's collating 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 with POSIX character sets such as [:alpha:].
The s
flag causes tr
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.
The d
flag causes tr
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'
The c
flag indicates the complement of the first set of characters. The invocation
tr -cd '[:alnum:]'
therefore removes all non-alphanumeric characters.
ImplementationsEdit
The original version of tr
was written by Douglas McIlroy and was introduced in Version 4 Unix.<ref name="reader">Template:Cite tech report</ref>
The version of tr
bundled in GNU coreutils was written by Jim Meyering.<ref>{{#invoke:citation/CS1|citation
|CitationClass=web
}}</ref> The command is available as a separate package for Microsoft Windows as part of the UnxUtils collection of native Win32 ports of common GNU Unix-like utilities.<ref>{{#invoke:citation/CS1|citation
|CitationClass=web
}}</ref> It is also available in the OS-9 shell.<ref>Template:Cite book</ref> A tr
command is also part of ASCII's MSX-DOS2 Tools for MSX-DOS version 2.<ref>MSX-DOS2 Tools User's Manual by ASCII Corporation</ref> The Template:Mono command has also been ported to the IBM i operating system.<ref>{{#invoke:citation/CS1|citation
|CitationClass=web
}}</ref>
Most versions of tr
, including GNU tr
and classic Unix tr
, operate on single-byte characters and are not Unicode compliant. An exception is the Heirloom Toolchest implementation, which provides basic Unicode support.
Ruby and Perl also have an internal tr operator, which operates analogously.<ref>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref><ref name=Perl_trDoc>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref> Tcl's string map command is more general in that it maps strings to strings while tr maps characters to characters.<ref name=TCLManualPage>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref>
See alsoEdit
ReferencesEdit
External linksEdit
- Template:Man
- tr(1) – Unix 8th Edition manual page.
- Template:Man
- Template:Man
- Template:Man
- Template:Man
- Template:Man
- usage examples at examplenow.com
Template:Unix commands Template:Plan 9 commands Template:Core Utilities commands