2つのファイルを結合します。DBで複数のテーブルを結合するのに似ています。適用できる場面ではとても便利です。 使用法: join [オプション]... ファイル1 ファイル2 For each pair of input lines with identical join fields, write a line to standard output. The default join field is the first, delimited by whitespace. When FILE1 or FILE2 (not both) is -, read standard input. -a FILENUM print unpairable lines coming from file FILENUM, where FILENUM is 1 or 2, corresponding to FILE1 or FILE2 -e EMPTY replace missing input fields with EMPTY -i, --ignore-case ignore differences in case when comparing fields -j FIELD equivalent to `-1 FIELD -2 FIELD' -o FORMAT obey FORMAT while constructing output line -t CHAR use CHAR as input and output field separator -v FILENUM like -a FILENUM, but suppress joined output lines -1 FIELD join on this FIELD of file 1 -2 FIELD join on this FIELD of file 2 --help この使い方を表示して終了 --version バージョン情報を表示して終了 Unless -t CHAR is given, leading blanks separate fields and are ignored, else fields are separated by CHAR. Any FIELD is a field number counted from 1. FORMAT is one or more comma or blank separated specifications, each being `FILENUM.FIELD' or `0'. Default FORMAT outputs the join field, the remaining fields from FILE1, the remaining fields from FILE2, all separated by CHAR. Important: FILE1 and FILE2 must be sorted on the join fields. E.g., use `sort -k 1b,1' if `join' has no options. ※「FILE1 and FILE2 must be sorted on the join fields.」と書いてありますが、必ずしも数値もしくはascii文字順で並んでいる必要はなく、一定の規則に従っていれば問題ないようです。
data1.txt
1 123 2 456 4 789 0 000 Z 789 A 123 data2.txt
1 abc 3 def 4 ghi 0 000 Z 444 A 123 実行例1
1 123 abc 4 789 ghi 0 000 000 Z 789 444 A 123 123 ちなみに規則性のない順だと正しく結合できません。 data1.txt
1 123 2 456 4 789 0 000 Z 789 A 123 data3.txt
1 abc A 123 4 ghi 0 000 3 def Z 444 実行例2
$ join data1.txt data3.txt 1 123 abc Z 789 444 こんな使い方がいいかもしれません。
log0.txt
00:00 00:01 00:02 00:03 00:04 log1.txt
00:01 アプリ起動 00:03 エラー発生 00:08 エラー発生 log2.txt
00:02 Aさん処理 00:03 Bさん処理 00:04 Bさん再処理 実行例3
$ join -a 1 log0.txt log1.txt > log3.txt $ join -a 1 log3.txt log2.txt 00:00 00:01 アプリ起動 00:02 Aさん処理 00:03 エラー発生 Bさん処理 00:04 Bさん再処理 2011/10/14
|
Linux >