The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
insert file2 after line containing patternX in file1 repudi8or Shell Programming and Scripting 5 04-18-2008 10:35 AM
Search, replace string in file1 with string from (lookup table) file2? gstuart Shell Programming and Scripting 2 04-11-2008 11:32 AM
match value from file1 in file2 myguess21 Shell Programming and Scripting 2 02-21-2008 08:39 AM
echo "ABC" > file1.txt file2.txt file3.txt ganapati UNIX for Dummies Questions & Answers 4 01-29-2008 09:36 PM
delete lines from file2 beginning w/file1 michieka Shell Programming and Scripting 13 06-24-2003 10:42 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 11-08-2007
RacerX's Avatar
Registered User
 

Join Date: Oct 2007
Posts: 34
Awk Compare f1,f2,f3 of File1 with f1 of File2

I have an Awk string-compare problem and have searched the internet and forums for a solution i could use but cannot find a solution i understand to make work with my particular problem:

I need to compare (field1 field2 field3 of File1) against (field1 of File2) and if they match print out (field1:field2:field3) of File1 followed by field2 of File2 and field3 of File2. In the case of no-match exists in File2, just print out the (field1:field2:field3 of File1).

File1
Code:
RICHARD:J:LOONEY
JAMES:F:BIXLER
JED:H:YOUNG
LEWIS:A:ZAPP
SILAS: :VECINIO
DERICK:S:HOLMER
MICK: :REZNIC
R: :BAKER
HERROD:G:LOST
OLIVE:N:TORROSSI
JASPER:G:WILCOX
AUDREY:H:VIKING
File2
Code:
RICHARD J LOONEY:YONKERS:NY  
JAMES F BIXLER:LEXINGTON:KY 
JED H YOUNG:SURREY:BC
LEWIS A ZAPP:GREEN VALLEY:CA
SILAS VECINIO:COLUMBUS:OH 
DERICK S HOLMER:WESTFORD:MA 
MICK REZNIC:AKRON:OH  
R BAKER:AUCKLAND:NEW ZEALAND
OLIVE N TORROSSI:DAVISON:MI
LEWIS A ZAPP:GREEN VALLEY:CA
JASPER G WILCOX:CANTON:OH 
AUDREY H VIKING:SURREY:BC
DESIRED OUTPUT
Code:
RICHARD:J:LOONEY:YONKERS:NY
JAMES:F:BIXLER:LEXINGTON:KY
JED:H:YOUNG:SURREY:BC
LEWIS:A:ZAPP:GREEN VALLEY:CA
SILAS: :VECINIO:COLUMBUS:OH 
DERICK:S:HOLMER:WESTFORD:MA 
MICK: :REZNIC:AKRON:OH  
R: :BAKER:AUCKLAND:NEW ZEALAND
HERROD:G:LOST
OLIVE:N:TORROSSI:DAVISON:MI
JASPER:G:WILCOX:CANTON:OH
AUDREY:H:VIKING:SURREY:BC
Reply With Quote
Forum Sponsor
  #2  
Old 11-08-2007
aigles's Avatar
Registered User
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,212
You can do something like that (assume that the two files two contain the same list of names) :
Code:
function are_same_names(n11, n12, n13, n2     ,n1) {
   n1 = n11 " " n12 " " n13;
   gsub(/ +/, " ", n1);
   return (n1 == n2)
}

BEGIN {
   FS = OFS = ":";

   while (getline < ARGV[1]) {
      f11 = $1;
      f12 = $2;
      f13 = $3;
      getline < ARGV[2];
      if (are_same_names(f11, f12, f13, $1)) {
         print f11, f12, f13, $2, $3;
      } else {
         print f11, f12, f13;
      }
   }
}
Command:
Code:
awk -f compare.awk File1 File2
Jean-Pierre.
Reply With Quote
  #3  
Old 11-08-2007
RacerX's Avatar
Registered User
 

Join Date: Oct 2007
Posts: 34
Thanks for your help and possible solution! After running your code against my two example files above it returns:

Code:
RICHARD:J:LOONEY:YONKERS:NY  
JAMES:F:BIXLER:LEXINGTON:KY 
JED:H:YOUNG:SURREY:BC
LEWIS:A:ZAPP:GREEN VALLEY:CA
SILAS: :VECINIO:COLUMBUS:OH 
DERICK:S:HOLMER:WESTFORD:MA 
MICK: :REZNIC:AKRON:OH  
R: :BAKER:AUCKLAND:NEW ZEALAND
HERROD:G:LOST
OLIVE:N:TORROSSI
JASPER:G:WILCOX:CANTON:OH 
AUDREY:H:VIKING:SURREY:BC
When you said
Quote:
You can do something like that (assume that the two files two contain the same list of names.
Do you mean the names are getting compared only once by line number, like Line1 File1 field1 is only compared to Line1 File2 field1 where it either matches or not, then goes on to Line2 File1 field1 compared to Line2 File2 field1 and matches or not, and then goes on to next line, etc, to the EOF?
Reply With Quote
  #4  
Old 11-08-2007
aigles's Avatar
Registered User
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,212
Quote:
Originally Posted by RacerX View Post
When you said Do you mean the names are getting compared only once by line number, like Line1 File1 field1 is only compared to Line1 File2 field1 where it either matches or not, then goes on to Line2 File1 field1 compared to Line2 File2 field1 and matches or not, and then goes on to next line, etc, to the EOF?
Yes.
If the two files may contains different names the awk program must be modified (the function are_same_names is allways valid).

Jean-Pierre.
Reply With Quote
  #5  
Old 11-08-2007
RacerX's Avatar
Registered User
 

Join Date: Oct 2007
Posts: 34
OK, that explains why i never could find a match in my real files using the above code, as the names in File1 i'm trying to match could be on any line in File2; so as you say i'll have to try to modify it to scan through every line in File2.

Thanks for getting me this far!
Reply With Quote
  #6  
Old 11-08-2007
aigles's Avatar
Registered User
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,212
Try and adapt the following awk program :
Code:
BEGIN {
   FS = ":";
}
NR==FNR {
   Names[$1] = $2 ":" $3;
   next;
}
{
   name = $1 " " $2 " " $3;
   gsub(/ +/, " ", name);
   if (name in Names) {
      print $0 ":" Names[name];
   } else {
      print $0 "
   }
}
Command;
Code:
awk -f compare.awk File2 File1
Jean-Pierre.
Reply With Quote
  #7  
Old 11-08-2007
Registered User
 

Join Date: Jun 2007
Location: Beijing China
Posts: 495
awk

Hi,
This one should be ok for you.

input:
Code:
a:
line1:a:A
line2:b:B
line3:c:C
line4:d:D
line5:e:E
b:
line6:f:F
line3:cc:CC
line5:ee:EE
line2:bb:BB
output:
Code:
line1:a:A
line2:b:B:bb:BB
line3:c:C:cc:CC
line4:d:D
line5:e:E:ee:EE
code:
Code:
nawk 'BEGIN{FS=":"}
{
if (NR==FNR)
	line[$1]=$0
else
	if (line[$1]!="")
	line[$1]=sprintf("%s:%s:%s",line[$1],$2,$3)
}
END{
for(i in line)
	print line[i] 
}' a b
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 07:22 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0