
tr (ティーアール)はUNIXおよびUNIX系システムのコマンドである。 tr (R-tee) is a UNIX command and the UNIX systems. 名称はtr anslateまたはtr ansliterateの略。 Tr ansliterate tr anslate the name or abbreviation.
trは標準入力から読み込んで標準出力に出力する。 tr is the standard output to read from standard input. パラメータとして2つの文字集合を指定し、一方の文字集合に含まれる文字が出現する度に、もう一方の文字集合の同じ位置にある文字に置換して出力する。 Two parameters specify the character set as one time occurrences of the characters in one character set to output to replace the characters in the same position of the other character sets.
次の例では、アルファベットをアルファベット順で7つ後の文字に全て置換する( aはhに)。 The following example is the alphabet in alphabetical order after 7 to replace all three characters (a at h).
$ echo cheer | tr abcdefghijklmnopqrstuvwxyz hijklmnopqrstuvwxyzabcdefg $ Echo cheer | tr abcdefghijklmnopqrstuvwxyz hijklmnopqrstuvwxyzabcdefg jolly jolly
使用しているtrがもしPOSIX準拠ならば、最後の2つの語は単にaz h-za-gと書くことができる。 Tr If you are using a POSIX-compliant if the last one just two words az h-za-g can be written.
"\n" を "\r\n" に置換するには、以下のようにすればよい。 "\ n" to "\ r \ n" to be replaced are as follows: How do I.
$ tr -A '\12' '\15\12' < input1 > output1 $ Tr-A '\ 12' '\ 15 \ 12' <input1> output1 $ tr -A '^M' '\15\12' < output1 > output2 $ Tr-A '^ M' '\ 15 \ 12' <output1> output2
ただし、すべてのtrが-Aオプションに対応しているわけではないことに注意してほしい。 However, not all that tr the-A option corresponds to keep in mind that it is not. また、単一引用符の代わりに二重引用符を使うことはできない。 Also, use double quotes instead of single quotes are not. シェルが逆スラッシュを解釈してしまうからだ。 It would shell interprets the backslash. ここで、\nや\12や^Jは、それぞれ文字エスケープ、 ASCII 8進、カレット表記を使った改行文字を示している。 Here, \ n or \ and ^ J is 12, each character escapes, ASCII 8 decimal, which indicates the newline character with a caret notation. \rや\15や^Mは、復帰文字である。 \ r and \ or 15 ^ M is carriage return. 背景については改行コードの項を参照。 For background see the section of line code.
次の例では、アルファベットをアルファベット順で1つ前の文字に全て置換する( aはzに)。 The following example is the alphabet in alphabetical order to replace all the previous characters (a is the z).
$ echo "ibm 9000" >computer.txt $ Echo "ibm 9000"> computer.txt $ tr az za-y <computer.txt $ Tr az za-y <computer.txt hal 9000 hal 9000
POSIX互換でない古いtrでは、文字の範囲指定を角括弧で囲む必要があり、 シェルが解釈するのを防ぐためにさらに引用符で囲む必要がある。 POSIX tr does not compatible with older, must be enclosed in square brackets in the specified range of characters, it must be enclosed in quotation marks to prevent the further interpreted by the shell.
$ tr "[az]" "z[ay]" <computer.txt $ Tr "[az]" "z [ay]" <computer.txt
どちらのバージョンが呼び出されるか不明な場合、この例では範囲指定ではなく全文字を並べて示す必要がある( tr abcdefghijklmnopqrstuvwxyz zabcdefghijklmnopqrstuvwxy )。 If you are unsure which version is called, in this case have shown that not all characters specified by range (tr abcdefghijklmnopqrstuvwxyz zabcdefghijklmnopqrstuvwxy). 使い方によっては古い記法で済む場合もある。 How will sometimes get away with the old notation. 例えば、 ROT13はtr "[AM][NZ][am][nz]" "[NZ][AM][nz][am]"となり、最近のバージョンでも問題なく動作する。 For example, ROT13 is tr "[AM] [NZ] [am] [nz]" "[NZ] [AM] [nz] [am]" Nearby, in recent versions work just fine. これは、角括弧が2つの文字集合の同じ位置にあるため、置換されても変化しないためであって、POSIX のtrが角括弧を解釈しないことには変わりない。 This is two square brackets in the same position for one set of characters, and not be replaced because it does not change even if, POSIX tr not to interpret the square brackets are frozen.
RubyとPerlにもtr演算子(メソッド)があり、同様の働きをする。 Ruby and Perl to the operator tr (methods), and similar to the work. 例えば、日本語を扱えるPerlを使うことにより、以下のPerlスクリプトは平仮名と片仮名とを交換する。 For example, Perl can handle the Japanese language by using the following exchange with Perl scripts hiragana and katakana. (ただし、「ヴ」「ヵ」「ヶ」を除く。) (However, "Vu", "months", "months" remove.)
tr /ぁ-んァ-ン/ァ-ンぁ-ん/; tr / A - A I - Ng / A - A Ng - I /; tr/ゝゞヽヾ/ヽヾゝゞ/ ; tr /ヾヽIsuzuゝ/ Isuzuゝヾヽ/;
| ||||||||||||||||||||||||||
