Bash:共通コマンドツール-trコマンド

8803 ワード

trコマンドは簡単な文字の置換と削除に使用できます.よく使われるのは-d、-sオプションです.置換と削除は単一の文字で行われます
次のテキストがあるとします.
Read from the file words.txt 
and output the word frequency list
to stdout.

USE CASE 1:
テキストをすべて大文字に変換
$ cat text.txt | tr [a-z] [A-Z]

READ FROM THE FILE WORDS.TXT

AND OUTPUT THE WORD FREQUENCY LIST

TO STDOUT.

trコマンドの[a-z]は置換が必要な文字の集合であり、[A-Z]は置換後の文字の集合であり、それらは一つ一つ対応する関係である(aはA、bはB...).trコマンドは、次のような文字列の置換作業を行うことはできません.
$ cat text.txt | tr "the" "an"

Rnad from ann filn words.axa

and ouapua ann word frnqunncy lisa

ao sadoua.

theはanに置き換えられるのではなく,再び文字ごとに置き換えられていることがわかる(t->a,h-->n,e->n),この点に注意が必要である.
USE CASE 2:
テキストの単語を1行1つに抽出
$ cat text.txt | tr -s " " "
" Read from the file words.txt and output the word frequency list to stdout.

ここでは-sパラメータを使用して重量を除去します.ここでの重量除去は、指定した文字が置き換えられた後、隣接する重複が見つかった場合、-sパラメータを追加しない場合は、1つだけ保持されます.
$ cat text.txt | tr  " " "
" Read from the file words.txt and output the word frequency list to stdout.

余ったレンタカーが出てくるのが見えます.もっと極端な例を挙げます.
$ echo "AAaaaaabbBBbb"|tr -s [ab] [AB]

AB

-sパラメータを使用すると、置換後に連続的に繰り返される文字が1つしか残っていないことがわかります.
path変数の表示:
$ echo $PATH

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games

$ echo $PATH|tr -s ":" "
" /usr/local/sbin /usr/local/bin /usr/sbin /usr/bin /sbin /bin /usr/games /usr/local/games

USE CASE 3:
指定文字の削除
$ date|tr -d [A-Za-z]

  17 14:45:56  2015

参照先:http://blog.sina.com.cn/s/blog_58c3f7960100uttl.html
NAME

       tr - translate or delete characters



SYNOPSIS

       tr [OPTION]... SET1 [SET2]



DESCRIPTION

       Translate, squeeze, and/or delete characters from standard input, writing to standard output.



       -c, -C, --complement

              use the complement of SET1



       -d, --delete

              delete characters in SET1, do not translate



       -s, --squeeze-repeats

              replace each input sequence of a repeated character that is listed in SET1 with a single occurrence of that character



       -t, --truncate-set1

              first truncate SET1 to length of SET2



       --help display this help and exit



       --version

              output version information and exit



       SETs are specified as strings of characters.  Most represent themselves.  Interpreted sequences are:



       \NNN   character with octal value NNN (1 to 3 octal digits)



       \\     backslash



       \a     audible BEL



       \b     backspace



       \f     form feed



       
new line \r return \t horizontal tab \v vertical tab CHAR1
-CHAR2 all characters from CHAR1 to CHAR2 in ascending order [CHAR*] in SET2, copies of CHAR until length of SET1 [CHAR*REPEAT] REPEAT copies of CHAR, REPEAT octal if starting with 0 [:alnum:] all letters and digits [:alpha:] all letters [:blank:] all horizontal whitespace [:cntrl:] all control characters [:digit:] all digits [:graph:] all printable characters, not including space [:lower:] all lower case letters [:print:] all printable characters, including space [:punct:] all punctuation characters [:space:] all horizontal or vertical whitespace [:upper:] all upper case letters [:xdigit:] all hexadecimal digits [=CHAR=] all characters which are equivalent to CHAR Translation occurs if -d is not given and both SET1 and SET2 appear. -t may be used only when translating. SET2 is extended to length of SET1 by repeating its last character as necessary. Excess characters of SET2 are ignored. Only [:lower:] and [:upper:] are guaranteed to expand in ascending order; used in SET2 while translating, they may only be used in pairs to specify case conversion. -s uses SET1 if not translating nor deleting; else squeezing uses SET2 and occurs after translation or deletion.