I have two files.

File 1:

A0001  C001
B0003  C896
A0024  C234
B1542  C231
upto 28412 such lines

File 2:

and 12000 such lines.

I want to compare File 2 against File 1 and store the matching lines from File 1. I tried Perl and Bash but none seems to be working.

The latest thing I tried was something like this:

for (@q) # after storing contents of second file in an array
        $line =`cat File1 | grep $_`; #directly calling File 1 from bash
        print $line;

but it fails.


You can use awk:

$ awk 'FNR==NR{a[$1];next}($1 in a){print}' file2 file1
A0001   C001
A0024   C234
B1542   C231
  • @user3543389 - why did you pick this one over the grep solution? I'm just curious. – slm Apr 17 '14 at 3:17
  • I have already tried that. see this stackoverflow.com/questions/23122636/… – user3543389 Apr 17 '14 at 3:19
  • Although this one still doesnt give output with larger files – user3543389 Apr 17 '14 at 3:21
  • @user3543389: Do you mean "this one" is my answer? A note in your perl solution that you should use while loop instead of for loop. – cuonglm Apr 17 '14 at 3:30

This should do the job:

grep -Ff File2 File1

The -f File2 reads the patterns from File2 and the -F treats the patterns as fixed strings (ie no regexes used).

  • Only downside with grep is that the awk solution is much faster for ~200 KB files. – Marcus Dec 28 '18 at 20:33

It looks to me like both files are already sorted on the first field. If so:

join file1 file2

is best, by about as far as your files are large.

  • Tried this; each file must be sorted for this to work. In grep solution that is not needed. – Matthew Turner Sep 13 '18 at 22:20
  • 2
    @МатиТернер yes, this is true, keeping large files sorted saves much time (when they're big enough to pirate the R out of RAM 'cause L2's not even close to enough) and keeping small files sorted is too cheap to meter anyway. – jthill Sep 24 '18 at 21:07

Not the answer you're looking for? Browse other questions tagged or ask your own question.