Tr (Unix)
{{short description|Unix text formatting utility}}
{{lowercase title}}
{{Infobox software
| name = tr
| logo =
| screenshot = Tr Unix.png
| screenshot size =
| caption = The {{code|tr}} command
| author = Douglas McIlroy
(AT&T Bell Laboratories)
| developer = Various open-source and commercial developers
| released = {{Start date and age|1973|11}}
| latest release version =
| latest release date =
| programming language = C
| operating system = Unix, Unix-like, Plan 9, Inferno, OS-9, MSX-DOS, IBM i
| platform = Cross-platform
| genre = Command
| license = coreutils: GPLv3+
Plan 9: MIT License
| website =
}}
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.
Overview
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.
Implementations
The original version of tr
was written by Douglas McIlroy and was introduced in Version 4 Unix.{{cite tech report |first1=M. D. |last1=McIlroy |author-link1=Doug McIlroy |year=1987 |url=http://www.cs.dartmouth.edu/~doug/reader.pdf |title=A Research Unix reader: annotated excerpts from the Programmer's Manual, 1971–1986 |series=Computing Science |number=139 |institution=AT&T Bell Laboratories |df=dmy-all }}
The version of tr
bundled in GNU coreutils was written by Jim Meyering.{{Cite web|url=https://linux.die.net/man/1/tr|title=Tr(1): Translate/Delete char - Linux man page}} 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.{{Cite web|url=http://unxutils.sourceforge.net/|title=Native Win32 ports of some GNU utilities|website=unxutils.sourceforge.net}} It is also available in the OS-9 shell.{{cite book|author=Paul S. Dayan|year=1992|title=The OS-9 Guru - 1 : The Facts|publisher=Galactic Industrial Limited|isbn=0-9519228-0-7}} A tr
command is also part of ASCII's MSX-DOS2 Tools for MSX-DOS version 2.[https://archive.org/details/MSXDOS2TOOLS MSX-DOS2 Tools User's Manual by ASCII Corporation] The {{Mono|tr}} command has also been ported to the IBM i operating system.{{cite web |title=IBM System i Version 7.2 Programming Qshell |language=en |author=IBM |website=IBM |author-link=IBM |url=https://www.ibm.com/support/knowledgecenter/ssw_ibm_i_74/rzahz/rzahzpdf.pdf?view=kc |access-date=2020-09-05 }}
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.{{cite web|title=tr (String) - APIdock|url=https://apidock.com/ruby/String/tr|publisher=APIdock|access-date=12 August 2015}}{{cite web|title=tr - perldoc.perl.org|url=https://perldoc.perl.org/functions/tr.html|publisher=perldoc.perl.org|access-date=12 August 2015}} Tcl's string map command is more general in that it maps strings to strings while tr maps characters to characters.{{cite web|title=Tcl Built-In Commands - string manual page|url=https://www.tcl.tk/man/tcl8.4/TclCmd/string.htm#M33|access-date=12 August 2015}}
See also
References
{{Reflist}}
External links
{{Wikibooks|Guide to Unix|Commands}}
- {{man|cu|tr|SUS}}
- [http://man.cat-v.org/unix_8th/1/tr tr(1)] – Unix 8th Edition manual page.
- {{man|1|tr|FreeBSD}}
- {{man|1|tr|Solaris}}
- {{man|1|tr|Linux}}
- {{man|1|tr|Plan 9}}
- {{man|1|tr|Inferno}}
- [https://web.archive.org/web/20120723113341/http://www.examplenow.com/tr/ usage examples at examplenow.com]
{{Unix commands}}
{{Plan 9 commands}}
{{Core Utilities commands}}
Category:Unix text processing utilities
Category:Unix SUS2008 utilities