Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Match on Field1 and Field 2. If no match on Field3, print

Status
Not open for further replies.

madasafish

Technical User
Jul 18, 2006
78
TH

File1:
DC000600CN003350647 290 1
DC000600CN003350647 291 33
DC000600CN003350647 292 0
DC000600CN003350647 293 0
DC000600CN003350647 294 0

File2:
DC000600CN003350647 290 2
DC000600CN003350647 291 33
DC000600CN003350647 292 0
DC000600CN003350647 293 0
DC000600CN003350647 294 0

Code:
FILE1=File1
FILE2=File2

nawk -v 'NR==FNR {a[$1]=$2;a[$2]=$3;next}
{
if ($1 in a && $2 == a[$1] && $3 != a[$2])
{
printf "%-30s %-10s %-10s %-10s\n",$1,$2,$3,a[$2]
atot=atot+$3
btot=btot+a[$2]
}
}END{
print "================================================================"
printf "%-30s %-10s %-10s %-10s\n","Total" ,"",atot,btot
print "================================================================"

}' $FILE1 $FILE2

When run I get:
================================================================
Total
================================================================

Any help appreciated.
Please bear in mind there are 1680655 lines to check in each file.

Thanks in advance,
madasafish




 
Hi

$1 is not unique, so your theory can not work. Must use $1 and $2 together :
Code:
awk '
BEGIN{NR==FNR}

{[red]a[$1,$2]=$3[/red];next}

[red]a[$1,$2] && a[$1,$2]!=$3[/red] {
  printf "%-30s %-10s %-10s %-10s\n",$1,$2,$3,a[$2]
  atot=atot+$3
  btot=btot+a[$2]
}
END{
  print "================================================================"
  printf "%-30s %-10s %-10s %-10s\n","Total" ,"",atot,btot
  print "================================================================"
}' FILE1 FILE2
Tested with [tt]gawk[/tt] and [tt]mawk[/tt].

Feherke.
 
What about this ?
nawk -v '
NR==FNR{a[$1","$2]=$3;next}
$1","$2 in a && $3!=a[$1","$2]{
printf "%-30s %-10s %-10s %-10s\n",$1,$2,$3,a[$1","$2]
atot+=$3
btot+=a[$1","$2]
}
END{
print "================================================================"
printf "%-30s %-10s %-10s %-10s\n","Total" ,"",atot,btot
print "================================================================"
}' $FILE1 $FILE2

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top