-
Notifications
You must be signed in to change notification settings - Fork 1
/
tr.1
141 lines (93 loc) · 4.59 KB
/
tr.1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
TR(1) Human68k ITA TOOLBOX User's Manual TR(1)
名称
tr - 文字を変換する
形式
tr [ -b ] [ -1BCNZ ] [ -cds ] [ -- ] [ string1 [ string2 ] ]
解説
tr は標準入力を読み取り,1文字ずつ置換あるいは削除しながら標
準出力に出力します.
通常はシフトJISの 2バイト文字も 1文字として文字単位に処理し
ますが,-1 オプションを指定するとあくまでも 1バイトずつの変
換を行います.
基本的に,標準入力から入力された文字のうち string1 にあるも
のが,対応する string2 の文字に置換されます.オプション -c,
-d,-s の組み合わせは次のように動作を調整します.
-c string1 にある文字を置換するのではなく,すべての文字の
うち string1 にない文字に対して,文字コードの若い順に
string2 をマップします.
-d 入力文字のうち string1 にあるもの(-c オプション指定時
は string1 にないもの)を削除します.
-s string2 にある文字の連続を 1文字に縮めて出力します.
string1 および string2 は,-b オプションを指定しなければ
UNIX System V の tr の流儀で解釈され,-b オプションを指定す
ると BSD UNIX の tr の流儀で解釈されます.
● -b オプションを指定しない(UNIX System V の流儀)
string1 および string2 の任意の位置で次の省略記法が可能
です.
[a-z] 文字 a から文字 z までの文字範囲(文字コード
順)を意味します.ただし文字コードが減少する
方向ではエラーとなります.
[a*n] 文字 a の n個の連続を意味します.n は,最初の
桁が 0 ならば 8進表記,そうでなければ10進表記
と解釈されます.n が 0 であるか省略されている
場合には 10000個の連続を意味します.
string2 が string1 よりも短い場合,string1 に残った文字
は変換されません.
● -b オプションを指定する(BSD UNIX の流儀)
string1 および string2 の任意の位置で次の省略記法が可能
です.
a-z 文字 a から文字 z までの文字範囲(文字コード
順)を意味します.ただし文字コードが減少する
方向では,この特別な意味は持ちません.たとえ
ば‘9-1’は文字‘9’,‘-’,‘1’の 3文字を
意味することになります.
string2 が string1 よりも短い場合は,その最後の文字で
string1 の長さまで拡張されます.
いずれの場合にも,エスケープ文字‘\’に続く文字コード(1桁以
上 3桁以下の 8進数)で文字を表すことができます.エスケープ文
字はまた,特別に解釈される文字(System V の様式では‘[’,
BSD の様式では‘-’,さらにエスケープ文字‘\’自身)を通常の
文字とするために使うことができます.
(-1 オプションを指定していないとき)2バイト文字を文字コード
で表すには,1バイトずつに分けて記述しなければなりません.
-N オプションを指定すると,本来の変換に先立って ^M^J コード
が自動的に ^J コードに変換されます.Human68k や MS-DOS にお
いて一般的に改行コードとして使用されている ^M^J コードを 1文
字として変換したい場合に有効です.
-B オプション,-C オプション,および -Z オプションの意味は
cat(1) と同じです.
例
次の例は,file1 に含まれるASCII数字をすべて‘?’に,アルファ
ベット大文字をすべて‘X’に,小文字をすべて‘x’に置換して
file2 に書き出します.
tr '[0-9][A-Z][a-z]' '[?*10][X*26][x*26]' <file1 >file2
次の例は,file1 に含まれる「。」を「.」に,「、」を「,」に,
「(」と「)」をそれぞれ「(」と「)」に置換して file2 に書き
出します.
tr '。、()' '.,()' <file1 >file2
次の例は,file1 から単語(ASCIIアルファベットの綴り)を抜き
出して 1行に 1つずつ file2 に書き出します.
tr -cs '[A-Z][a-z]' '[\012*]' <file1 >file2
または
tr -b -cs A-Za-z '\012' <file1 >file2
\012 は改行コード(^J)を表します.file1 の改行コードが ^M^J
であるならば -N オプションを指定する必要があります.file2 の
改行コードを ^M^J にしたければ -C オプションを指定する必要が
あります.
なお,以上の例では,fish(1) などのシェルからの実行を想定して,
‘\’や‘[’などの文字をシェルから保護するために引用符を使用
しています.
関連事項
cat(1), fish(1)
診断
何らかのエラーがあったときにはエラー・メッセージが標準エラー
出力に出力されます.
終了ステータスとしては,引数の記述に間違いがあった場合には 1,
メモリ不足など,システムの問題により正常に実行ができなかった
場合には 3,すべてが正常に終了したならば 0 が返ります.^C な
どによって中断したときの終了ステータスは OS の仕様によります.
バージョン
1.2
制作・著作
Copyright(C)1993 by 板垣 史彦 (Itagaki Fumihiko)
1.2 Last change: 08 Jan 1994 Itagaki Fumihiko