Compare two files for matching lines and store positive results
I have two files.
File 1:
A0001 C001 B0003 C896 A0024 C234 . B1542 C231 . upto 28412 such lines
File 2:
A0001 A0024 B1542 . . 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.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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).Method 2
You can use awk
:
$ awk 'FNR==NR{a[$1];next}($1 in a){print}' file2 file1 A0001 C001 A0024 C234 B1542 C231
Method 3
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.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0