Return first two columns if match found among two files

Hi,

I have FileA with one column.
File B with 15 columns separated by comma delimiter.

I need to compare the FILEA value with all 15 columns of FILEB... if matches, need to return the 1st, 2nd column values of FILEB.

How to achieve this through shell script? Thanks in advance.

Can you please provide sample input and expected output data? What have you tried so far?

So just to understand your requiements...
FileA contains one column.
FileB contains 15 columns.

What do you mean by "compare the FILEA value"?

Am I right in assuming each of the columns in FileB contains only one record? And that the one column in FileA contains 15 rows? I.E. Being a vertical representation of FileB?

Hi,

FileA is having a single column with 1000 rows.
FileB is having 15 columns with 13000 rows.

Need to compare File A values with FileB, and if that comparing value matches with any of the value from all 15 columns of fileB, it should return the 1st, 2nd columns of FileB among 15 columns.

Try Something like this

awk 'FNR==NR{A[$1];next}{for(i=1;i<=NF;i++){ if($i in A){ print $1,$2 ; next  } }}' FILEA FILEB
3 Likes

Just this little detail in case our friend doesn't know how to add a Field Separator,

1 Like

Thanks Akshay & Kibou. That code worked! :slight_smile: