Reputation: 71
I have got two files, say file1
and file2
, to be compared and put it in file3
.
file1
:
red
green
blue
red
yellow
pink
orange
file2
:
domain1,red,-
domain2,-,green
domain3,blue,-
domain4,yellow,pink
domain5,grey,orange
Now I need output as follows in file3
:
domain1,red
domain2,green
domain3,blue
domain1,red
domain4,yellow
domain4,pink
domain5,orange
For each record in file1
, if matched, it needs to produce $1
as mandatory and the matching value either $2
or $3
from file2
, if both($2,$3) are matched, then it should be in two records (single record also would do) as "domain5,pink,orange".
I'm newbie to awk command. Please help me achieve this with awk
!
I have this simple command which is not sufficing my condition
awk 'NR==FNR{x[$1];next}($2,$3) in x' FS=',' file1 file2 >file3
Upvotes: 2
Views: 896
Reputation: 97928
awk -F, 'BEGIN{OFS=","}
NF>1 {s[$2]=s[$3]=$1;}
NF==1 {print s[$1],$1}' input2 input1
Output
domain1,red
domain2,green
domain3,blue
domain1,red
domain4,yellow
domain4,pink
domain5,orange
Upvotes: 3