Visit Our UNIX and Linux User Community


Awk Compare f1,f2,f3 of File1 with f1 of File2


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Awk Compare f1,f2,f3 of File1 with f1 of File2
# 1  
Old 11-08-2007
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

# 2  
Old 11-08-2007
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.
# 3  
Old 11-08-2007
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?
# 4  
Old 11-08-2007
Quote:
Originally Posted by RacerX
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.
# 5  
Old 11-08-2007
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!
# 6  
Old 11-08-2007
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.
# 7  
Old 11-09-2007
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


Previous Thread | Next Thread
Test Your Knowledge in Computers #561
Difficulty: Easy
GDB is a command line debugger.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to update field in file2 if not the same as file1

Trying to use awk to: update $2 in file2 with the $2 value in file1, if $1 in file1 matches $13 in file2, which is tab-delimeted. The $2values may already be the same so in that case nothing happens and the next line is processed. There are exactly 4,605 unique $13 values. Thank you :). ... (4 Replies)
Discussion started by: cmccabe
4 Replies

2. Shell Programming and Scripting

awk to search field2 in file2 using range of fields file1 and using match to another field in file1

I am trying to use awk to find all the $2 values in file2 which is ~30MB and tab-delimited, that are between $2 and $3 in file1 which is ~2GB and tab-delimited. I have just found out that I need to use $1 and $2 and $3 from file1 and $1 and $2of file2 must match $1 of file1 and be in the range... (6 Replies)
Discussion started by: cmccabe
6 Replies

3. UNIX for Dummies Questions & Answers

Compare file1 and file2, print matching lines in same order as file1

I want to print only the lines in file2 that match file1, in the same order as they appear in file 1 file1 file2 desired output: I'm getting the lines to match awk 'FNR==NR {a++}; FNR!=NR && a' file1 file2 but they are in sorted order, which is not what I want: Can anyone... (4 Replies)
Discussion started by: pathunkathunk
4 Replies

4. Shell Programming and Scripting

Compare and find records of file1 not in file2

hi.. i am using solaris system and ksh and using nawk to get records of file1 not in file2(not line by line comparison). code i am using is nawk 'NR==FNR{a++} !a {print"line:" FNR"->" $0} ' file2 file1 same command with awk runs perfectly on darwin kernel(mac) but in solaris it does line by... (2 Replies)
Discussion started by: Abhiraj Singh
2 Replies

5. Shell Programming and Scripting

Looking for lines, which is present in file1 but not in file2 using UNIX and awk

I have 2 files with 7 fields and i want to print the lines which is present in file1 but not in file2 based on field1 and field2. Logic: I want to print all the lines, where there is a particular column1 and column2. And we do not find the set of column1 and column2 in file2. Example: "sc2/10... (3 Replies)
Discussion started by: NamS
3 Replies

6. Shell Programming and Scripting

[awk] split file1 and save it as var from file2

I have 2 files: file_1: file_2: expected result: name file: "artV1" "artV2" etc. I have: but why don;t work save to file 'out'?? (3 Replies)
Discussion started by: ffresz
3 Replies

7. UNIX for Dummies Questions & Answers

if matching strings in file1 and file2, add column from file1 to file2

I have very limited coding skills but I'm wondering if someone could help me with this. There are many threads about matching strings in two files, but I have no idea how to add a column from one file to another based on a matching string. I'm looking to match column1 in file1 to the number... (3 Replies)
Discussion started by: pathunkathunk
3 Replies

8. Shell Programming and Scripting

AWK: Pattern match between 2 files, then compare a field in file1 as > or < field in file2

First, thanks for the help in previous posts... couldn't have gotten where I am now without it! So here is what I have, I use AWK to match $1 and $2 as 1 string in file1 to $1 and $2 as 1 string in file2. Now I'm wondering if I can extend this AWK command to incorporate the following: If $1... (4 Replies)
Discussion started by: right_coaster
4 Replies

9. Shell Programming and Scripting

awk - replacing stings in file1 with column1 in file2

Hello, I've never used awk before, but from what I've read, it will best suit what I'm trying to do. I have 2 files. I need to replace strings in file1 with the first column of a matching string in file2. Below are examples: File1: random-string1 1112 1232 3213 2131 random-string2... (7 Replies)
Discussion started by: upstate_boy
7 Replies

10. Shell Programming and Scripting

Awk Compare File1 File2 on f2

I'm trying to compare two files using AWK, where if field2 of both files match, replace field1 of file1 with field1 of file2 and if there is no match just print the line of file1. file1.txt (has empty first field) :ABBATOM:B:H:1992 :ABBA TROJAN:B:H:1993 :ABBES FIRST HOPE:B:M:1997 :ABBEYS... (4 Replies)
Discussion started by: RacerX
4 Replies

Featured Tech Videos