comparing in files and output

i have a file a with contents

tom   
lasole
jon   
gille
sam 

and a file b with contents

tom|1234|abcf|newyork,ohio,oregon
sam|2345|drft|texas,london
hyle|4444|befr|wisconsin
neyo|2333|tdtt|ohio,jersey

i want to compare records in file a to file b such that when the name tom is found in file b column 1 and has either newyork or ohio or oregon IN Column 3 display as

tom|NOO

else display

sam|YES.

so the output of file a after comparison should be

tom | NOO  
lasole|YES
jon|YES   
gille|YES
sam|YES

since tom has either newyork or ohio or oregon in column3 of file b

A simple script should suffice:-

#!/bin/ksh
for ref in `cat filea`
do
   cities=`grep "^$ref fileb"|cut -f3 -d"|"|tr "," " "`
   match="NOO"
   for city in $cities
   do
      if [ "$city" = "newyork" -o "$city" = "ohio" -o "$city" = "oregon" ]
      then
         match="YES"
      fi
   done
   echo "$ref|$match"
done

I hope that this helps, but your desired output is a little inconcistent, so I've had to guess. have a play and let us all know how you get on or if you need more help.

Robin
Liverpool/Blackburn
UK

its not working

It would appear there is a quote in the wrong place.

grep "^$ref fileb"

should be

grep "^$ref" fileb

Output now is:

tom|NOO
lasole|NOO
jon|NOO
gille|NOO
sam|NOO

If that doesn't solve your problem, please say what doesn't work.

Just saying "It doesn't work" is really no use to anyone.

output should be

tom | NOO 
lasole|YES
jon|YES 
gille|YES
sam|YES

Many thanks Scott. I am a fool in my coding.:rolleyes: I read the text saying column 3 and just coded it straight. :o

So, dealerso, now with proper testing, I found that my logic was in reverse too.

Amended & testing code appended:-

for ref in `cat filea`
do
   cities=`grep "^$ref" fileb|cut -f4 -d"|"|tr "," " "`
   match="YES"
   for city in $cities
   do
      if [ "$city" = "newyork" -o "$city" = "ohio" -o "$city" = "oregon" ]
      then
         match="NOO"
      fi
   done
   echo "$ref|$match"
done

From the input given, I get the output:-

tom|NOO
lasole|YES
jon|YES
gille|YES
sam|YES

Not quite what is shown, but that would be more to code, unless you really want the spaces around the pipe mark for record tom.

Does that fix things up? Let us all know if this is the answer or if I have missed something. :b:

Robin
Liverpool/Blackburn
UK