tr is a
command in
Unix
Unix (; trademarked as UNIX) is a family of multitasking, multiuser computer operating systems that derive from the original AT&T Unix, whose development started in 1969 at the Bell Labs research center by Ken Thompson, Dennis Ritchie, a ...
,
Plan 9 Plan 9 or Plan Nine may refer to:
Music
* Plan 9 (band), a psychedelic rock band from Rhode Island
* ''Plan 9'', an album by Big Guitars From Memphis with Rick Lindy
* "Plan 9", a song on the 1993 album ''Gorgeous'' by electronica band 808 Stat ...
,
Inferno, and
Unix-like
A Unix-like (sometimes referred to as UN*X or *nix) operating system is one that behaves in a manner similar to a Unix system, although not necessarily conforming to or being certified to any version of the Single UNIX Specification. A Unix-li ...
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
Collation is the assembly of written information into a standard order. Many systems of collation are based on numerical order or alphabetical order, or extensions and combinations thereof. Collation is a fundamental element of most office filin ...
, 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.
The version of
tr
bundled in
GNU coreutils
The GNU Core Utilities or coreutils is a package of GNU software containing implementations for many of the basic tools, such as cat, ls, and rm, which are used on Unix-like operating systems.
In September 2002, the ''GNU coreutils'' were c ...
was written by Jim Meyering. 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. It is also available in the
OS-9 shell. A
tr
command is also part of
ASCII
ASCII ( ), abbreviated from American Standard Code for Information Interchange, is a character encoding standard for electronic communication. ASCII codes represent text in computers, telecommunications equipment, and other devices. Because ...
's ''MSX-DOS2 Tools'' for
MSX-DOS version 2. The command has also been ported to the
IBM i operating system.
Most versions of
tr
, including GNU
tr
and classic Unix
tr
, operate on single-byte characters and are not
Unicode
Unicode, formally The Unicode Standard,The formal version reference is is an information technology standard for the consistent encoding, representation, and handling of text expressed in most of the world's writing systems. The standard, ...
compliant. An exception is the
Heirloom Toolchest implementation, which provides basic Unicode support.
Ruby
A ruby is a pinkish red to blood-red colored gemstone, a variety of the mineral corundum (aluminium oxide). Ruby is one of the most popular traditional jewelry gems and is very durable. Other varieties of gem-quality corundum are called sapp ...
and
Perl
Perl is a family of two High-level programming language, high-level, General-purpose programming language, general-purpose, Interpreter (computing), interpreted, dynamic programming languages. "Perl" refers to Perl 5, but from 2000 to 2019 it ...
also have an internal ''tr'' operator, which operates analogously.
Tcl's ''string map'' command is more general in that it maps strings to strings while tr maps characters to characters.
See also
*
sed
*
List of Unix commands
This is a list of Unix commands as specified by IEEE Std 1003.1-2008, which is part of the Single UNIX Specification (SUS). These commands can be found on Unix operating systems and most Unix-like operating systems.
List
See also
* List of G ...
*
GNU Core Utilities
References
External links
*
tr(1)– Unix 8th Edition manual page.
*
*
*
*
*
usage examples at examplenow.com
{{Core Utilities commands
Unix text processing utilities
Unix SUS2008 utilities
Plan 9 commands
Inferno (operating system) commands
IBM i Qshell commands