Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This question already has an answer here:

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.

share|improve this question

marked as duplicate by jasonwryan, slm, Anthon, goldilocks, devnull Apr 17 '14 at 7:42

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

up vote 2 down vote accepted

You can use awk:

$ awk 'FNR==NR{a[$1];next}($1 in a){print}' file2 file1
A0001   C001
A0024   C234
B1542   C231
share|improve this answer
Works well!! Thanks! – user3543389 Apr 17 '14 at 3:16
@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).

share|improve this answer

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.

share|improve this answer

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