I have 2 files. One file contains a minimum of two fields (a username, an age, and a variable number of "fruits" each in their own field) per line, the other file always contains two fields per line (the username and one "fruit"). I would call file1
the "database" file and file2
the "mapping" file.
For each username I want to check if any of the lines in file1
corresponding to that user contains a "fruit" that is not mapped to that user as per file2
.
Example:
file1
(the number of fruits in each row is variable in this file):james,25,strawberry,rassberry,blueberry james,25,strawberry,rassberry,mango james,26,blueberry james,27,pineapple erik,30,strawberry,rassberry,mango
file2
:james,strawberry james,rassberry james,blueberry erik,blueberry erik,rassberry
Desired output:
james,25,strawberry,rassberry,mango james,27,pineapple erik,30,strawberry,rassberry,mango
These lines are selected because
file2
contains no associations ofmango
orpineapple
with userjames
and no association ofstrawberry
andmango
with usererik
.
I was only able to get partial solution with my code. I have tried below, but this is not checking for other columns in the same row.
awk 'BEGIN{FS=OFS=","}NR==FNR{a[$1]=$2;next}
{if (a[$1] && (a[$1]!=$3)){print $0, a[$1]}}' file2 file1
james,27,pineapple
outputted? Why isn'tjames,25,strawberry,rassberry,blueberry
outputted?