Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

tr (Unix)

From Wikipedia, the free encyclopedia
Unix text formatting utility
tr
Thetr command
Original authorsDouglas McIlroy
(AT&T Bell Laboratories)
DevelopersVariousopen-source andcommercial developers
Initial releaseNovember 1973; 52 years ago (1973-11)
Written inC
Operating systemUnix,Unix-like,Plan 9,Inferno,OS-9,MSX-DOS,IBM i
PlatformCross-platform
TypeCommand
Licensecoreutils: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.

Overview

[edit]

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.

Implementations

[edit]

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]

See also

[edit]

References

[edit]
  1. ^McIlroy, M. D. (1987).A Research Unix reader: annotated excerpts from the Programmer's Manual, 1971–1986(PDF) (Technical report). Computing Science. AT&T Bell Laboratories. 139.
  2. ^"Tr(1): Translate/Delete char - Linux man page".
  3. ^"Native Win32 ports of some GNU utilities".unxutils.sourceforge.net.Archived from the original on 2006-02-09. Retrieved2025-08-11.
  4. ^Paul S. Dayan (1992).The OS-9 Guru - 1 : The Facts. Galactic Industrial Limited.ISBN 0-9519228-0-7.
  5. ^MSX-DOS2 Tools User's Manual by ASCII Corporation
  6. ^IBM."IBM System i Version 7.2 Programming Qshell"(PDF).IBM.Archived(PDF) from the original on 2020-09-18. Retrieved2020-09-05.
  7. ^"tr (String) - APIdock". APIdock. Retrieved12 August 2015.
  8. ^"tr - perldoc.perl.org". perldoc.perl.org. Retrieved12 August 2015.
  9. ^"Tcl Built-In Commands - string manual page".Archived from the original on 15 April 2018. Retrieved12 August 2015.

External links

[edit]
The WikibookGuide to Unix has a page on the topic of:Commands
File system
Processes
User environment
Text processing
Shell builtins
Searching
Documentation
Software development
Miscellaneous
File system
Processes
User environment
Text processing
Shell builtins
Networking
Searching
Software development
Miscellaneous
File system
Text utilities
Shell utilities
Retrieved from "https://en.wikipedia.org/w/index.php?title=Tr_(Unix)&oldid=1336333404"
Categories:
Hidden categories:

[8]ページ先頭

©2009-2026 Movatter.jp