join
文字處理
用法
join -1 1 -2 1 [file1] [file2]按共同欄位把兩個檔案連起來,就像資料庫的 join;兩邊必須先按該欄位排好序,否則會悄悄掉行。
常用選項
| 選項 | 含義 |
|---|---|
| -1 | Which field to use in the first file |
| -2 | Which field to use in the second file |
| -t | The field separator, as in -t, |
| -a | Also print unpaired lines from that file, an outer join |
| -v | Print only the unpaired lines from that file |
| -e | Fill missing fields with this string |
| -o | Choose which fields the output holds |
範例
join -t, -1 1 -2 1 a.csv b.csvInner join on the first column.
join -a1 left.txt right.txtKeeps the unmatched lines from the left file.
它們本來就是用管線串起來用的。串三條往往比讓一條命令做完所有事更短。
怎麼看
- 方括號 [ ] 表示這部分可以不寫。
- 省略號 … 表示可以寫多個。
- 選項區分大小寫——有些命令裡 -r 和 -R 不是一回事。
常見問題
Q. join 是做什麼的?
按共同欄位把兩個檔案連起來,就像資料庫的 join;兩邊必須先按該欄位排好序,否則會悄悄掉行。
Q. 怎麼寫?
join -1 1 -2 1 [file1] [file2] —— 方括號表示可以省略的部分。
Q. 值得記的選項有幾個?
這裡列了 7 個,完整列表在 man join。這條命令屬於文字處理。
相關命令
man 手冊: man join