I do not see a direct problem with the buffering, as it seems to work in this example ::
echo "172.217.22.132\n172.217.22.132" | \
awk '{CIP=$1}
(buffer[CIP]==0) { print "Calling geoiplookup";
cmd="geoiplookup "CIP;
cmd | getline buffer[CIP];
close(cmd) }
{print buffer[CIP]}'
This produces:
Calling geoiplookup
GeoIP Country Edition: US, United States
GeoIP Country Edition: US, United States
As you see it is called only once, so the buffering works.
There is however a bug in your code, the following should work better.
awk 'BEGIN{OFS=","}
{ FS="\""; $0=$0; CIP=$4; }
(buffer[CIP]==0) { cmd="geoiplookup "CIP; cmd | getline buffer[CIP]; close(cmd) }
...
{print "CIP,..." >> mysql.infile }' $1
I think the main problem here is the understanding of the awk
syntax.
In its simple form, you should understand awk as a line
or record
parser. The awk language is build out of a set of rules of the form
pattern1 { action1 }
pattern2 { action2 }
...
Which should be interpreted as : if pattern1
is satisfied, perform action 1
on the current line. then continue to pattern2
. If no pattern is given, it assumes to be true
and the corresponding action is executed.
In the first example above, there are 3 rules.
{CIP=$1}
states that as a first action put the variable CIP
to $1
(buffer[CIP]==0) { print ... }
states that the second action (print ...
) is only performed if buffer[CIP]
is zero, i.e. the value is not yet computed.
- the final rule states print the buffered value.
I hope this helped.