tr
文字處理
用法
tr '[set1]' '[set2]'逐個字元地替換或刪除;它只讀標準輸入,直接給檔名會失敗,必須用 < 重新導向或用管線餵給它。
常用選項
| 選項 | 含義 |
|---|---|
| -d | Delete every character in the set |
| -s | Squeeze repeats of a character into one |
| -c | Use the complement of the set |
| -cd | Delete everything that is not in the set |
範例
tr 'a-z' 'A-Z' < notes.txtUppercases everything.
tr -d '\r' < win.txt > unix.txtStrips CR, turning CRLF endings into LF.
tr -s ' ' < spaced.txtCollapses runs of spaces into one.
它們本來就是用管線串起來用的。串三條往往比讓一條命令做完所有事更短。
怎麼看
- 方括號 [ ] 表示這部分可以不寫。
- 省略號 … 表示可以寫多個。
- 選項區分大小寫——有些命令裡 -r 和 -R 不是一回事。
常見問題
Q. tr 是做什麼的?
逐個字元地替換或刪除;它只讀標準輸入,直接給檔名會失敗,必須用 < 重新導向或用管線餵給它。
Q. 怎麼寫?
tr '[set1]' '[set2]' —— 方括號表示可以省略的部分。
Q. 值得記的選項有幾個?
這裡列了 4 個,完整列表在 man tr。這條命令屬於文字處理。
相關命令
man 手冊: man tr