first we sort the files on the third field :
sort -k 3 file1 > file1.sorted
sort -k 3 file2 > file2.sorted
then we get common values on the 3rd field using comm :
comm -12 <(cut -d " " -f 3 file1.sorted | uniq) <(cut -d " " -f 3 file2.sorted | uniq) > common_values.field
now we can join each sorted file on the common values :
join -1 3 -o '1.1,1.2,1.3,1.4' file1.sorted common_values.field > file.joined
join -1 3 -o '1.1,1.2,1.3,1.4' file2.sorted common_values.field >> file.joined
output is formated so we get the same field order as the one used in the files.
Standard unix tools used : sort, comm, cut, uniq, join.
The <( )
works with bash, for other shells you might use temp files instead.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…