The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Advanced & Expert Users
Google UNIX.COM



Thread: Compare files
View Single Post in UNIX Forums - Click on the Thread or Permalink to View Entire Thread -->
  #4 (permalink)  
Old 03-10-2007
cfajohnson's Avatar
cfajohnson cfajohnson is offline
Registered User
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 471
Quote:
Originally Posted by kharen11
Hi Masters! I know this problem is quite difficult.
I have two files that looked like this:

File1
mary a b d
anne d e
jane g h
sam a


File2
role1 a d c d
role2 e f g h
role3 a b d
role4 a d e
role5 g h


It will first look into file1 then compare to all entries in file2 regardless of arrangement.

The output should be:
mary role1 role3
anne role 4
jane role2 role5
sam role1 role3 role4


I've tried some other way to do this but unsuccessful. Can it be done in UNIX?

What are the criteria? This script looks for all lines in File2 that contain any of the letters after the name, but the ouput doesn't match yours:

Code:
while read name a b c d e
do
  [ -n "$a$b$c$d$e" ] &&
  result=$( grep ${a:+-e " $a"} ${b:+-e " $b"} ${c:+-e " $c"} \
             ${d:+-e " $d"} ${e:+-e " $e"} File2 | cut -d ' ' -f1)
  set -- $result
  echo "$name" $result
done < File1
Reply With Quote