Compare two files and extract

Assume we have two files - FileA and FileB. Content of files are as shown below :

FileA:

1001,value1,value4,value8,value9
1002,value4,value32,value46,value33
1503,value5,value45,value68,value53
1605,value4,value67,value56,value57
1073,value5,value45,value68,value53
1005,value4,value67,value56,value57
1006,value2,value75,value35,value79

FileB:

1001,value741,value474,value2568,value479
1002,value36,value3452,value3736,value145
1503,value47,value55,value68,value53
1605,value86,value87,value86,value5367
1073,value85,value85,value98,value333

Based on first column from FileB, compare , with FileA and extract the data from FileA and store in new file, the expected output should be:

1001,value1,value4,value8,value9
1002,value4,value32,value46,value33
1503,value5,value45,value68,value53
1605,value4,value67,value56,value57
1073,value5,value45,value68,value53

FileA will have more than 1 million lines [all unique] and FileB will be 50k [all unique lines], so, based on first column from FileB, extract from FileA. please advice the awk.

Please use code tags as required by forum rules!

Try

sed 's/^/^/;s/,.*$/,/' file2 | grep -f- file1

Hello alnhk,

Following may help.

awk -F"," 'NR==FNR{a[$1]=$0;next} ($1 in a){print $0}' FileB FileA

Output will be as follows.

1001,value1,value4,value8,value9
1002,value4,value32,value46,value33
1503,value5,value45,value68,value53
1605,value4,value67,value56,value57
1073,value5,value45,value68,value53

Thanks,
R. Singh

Hello alnhk,

I have a few to questions pose in response first:-

  • Is this homework/assignment? There are specific forums for these.
  • What have you tried so far?
  • What output/errors do you get?
  • What OS and version are you using?
  • What are your preferred tools? (C, shell, perl, awk, etc.)
  • What logical process have you considered? (to help steer us to follow what you are trying to achieve)

Most importantly, What have you tried so far?

There are probably many ways to achieve most tasks, so giving us an idea of your style and thoughts will help us guide you to an answer most suitable to you so you can adjust it to suit your needs in future.

We're all here to learn and getting the relevant information will help us all.

Thanks, in advance,
Robin