Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

comparing two unsorted files

Tags:

bash

shell

I have two tab separated files (please see the examples below):

File 1

Java    RAJ
PERL    ALEX
PYTHON  MAurice

(and so on)

File 2

ALEX    3.4
SAM     8.9
PEPPER  9.0

Now, if for instance say ALEX is also found in file 2 (it is not for sure that ALEX will be found) I should have a third file looking like this:

PERL ALEX 3.4

The code should check for all the values in column 2 of file 1 in file2.

Any suggestions for a bash script?

like image 540
Angelo Avatar asked Jul 14 '11 16:07

Angelo


1 Answers

You want to use join for that. First you need to sort according to join field though:

join -1 2 -2 1 <(sort +1 -2 file1) <(sort +0 -1 file2)
like image 193
mhyfritz Avatar answered Oct 23 '22 07:10

mhyfritz