![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
||||
|
||||
|
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 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 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 |
| Forum Sponsor | ||
|
|
|
#2
|
||||
|
||||
|
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;
}
}
}
Code:
awk -f compare.awk File1 File2 |
|
#3
|
||||
|
||||
|
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 Quote:
|
|
#4
|
||||
|
||||
|
Quote:
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
|
||||
|
||||
|
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
|
||||
|
||||
|
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 "
}
}
Code:
awk -f compare.awk File2 File1 |
|
#7
|
|||
|
|||
|
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 Code:
line1:a:A line2:b:B:bb:BB line3:c:C:cc:CC line4:d:D line5:e:E:ee:EE 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
|
|||
| Google The UNIX and Linux Forums |