首页·终端命令

join

文本处理

用法

join -1 1 -2 1 [file1] [file2]

按共同字段把两个文件连起来,就像数据库的 join;两边必须先按该字段排好序,否则会悄悄丢行。

常用选项

选项含义
-1Which field to use in the first file
-2Which field to use in the second file
-tThe field separator, as in -t,
-aAlso print unpaired lines from that file, an outer join
-vPrint only the unpaired lines from that file
-eFill missing fields with this string
-oChoose which fields the output holds

示例

join -t, -1 1 -2 1 a.csv b.csv

Inner join on the first column.

join -a1 left.txt right.txt

Keeps 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