coreutils: Paired and unpaired lines
1
1 8.3.4 Controlling ‘join’’s field matching
1 -----------------------------------------
1
1 In this section the ‘sort’ commands are omitted for brevity. Sorting
1 the files before joining is still required.
1
1 ‘join’’s default behavior is to print only lines common to both input
1 files. Use ‘-a’ and ‘-v’ to print unpairable lines from one or both
1 files.
1
1 All examples below use the following two (pre-sorted) input files:
1
1 $ cat file1 $ cat file2
1 a 1 a A
1 b 2 c C
1
1 Command Outcome
1
1 --------------------------------------------------------------------------
1 $ join file1 file2 common lines (_intersection_)
1 a 1 A
1 $ join -a 1 file1 file2 common lines _and_ unpaired lines
1 a 1 A from the first file
1 b 2
1 $ join -a 2 file1 file2 common lines _and_ unpaired lines
1 a 1 A from the second file
1 c C
1 $ join -a 1 -a 2 file1 file2 all lines (paired and unpaired)
1 a 1 A from both files (_union_).
1 b 2 see note below regarding ‘-o
1 c C auto’.
1
1 $ join -v 1 file1 file2 unpaired lines from the first file
1 b 1 (_difference_)
1
1 $ join -v 2 file1 file2 unpaired lines from the second
1 c C file (_difference_)
1
1 $ join -v 1 -v 2 file1 file2 unpaired lines from both files,
1 b 2 omitting common lines (_symmetric
1 c C difference_).
1
1
1 The ‘-o auto -e X’ options are useful when dealing with unpaired lines.
1 The following example prints all lines (common and unpaired) from both
1 files. Without ‘-o auto’ it is not easy to discern which fields
1 originate from which file:
1
1 $ join -a 1 -a 2 file1 file2
1 a 1 A
1 b 2
1 c C
1
1 $ join -o auto -e X -a 1 -a 2 file1 file2
1 a 1 A
1 b 2 X
1 c X C
1
1 If the input has no unpairable lines, a GNU extension is available;
1 the sort order can be any order that considers two fields to be equal if
1 and only if the sort comparison described above considers them to be
1 equal. For example:
1
1 $ cat file1
1 a a1
1 c c1
1 b b1
1
1 $ cat file2
1 a a2
1 c c2
1 b b2
1 $ join file1 file2
1 a a1 a2
1 c c1 c2
1 b b1 b2
1