pokero
pokero

Reputation: 1029

awk adding on zeroes

I have this command to parse a log file, and show each IP address and number of times the IP address is in the log file.

for x in $(cat /var/log/varnish/varnishncsa.log | awk '{print $1}' | uniq); do cat /var/log/varnish/varnishncsa.log | awk -v q=, "/$x/ { count++ } END { print $x q count }";done

It works fine, except AWK is adding on zero's to the output.

e.g. output is

198.530.980.148,3
72.140.1990.7,29

instead it should be:

198.53.98.148,3
72.14.199.7,29

I.e. 198.53.98.148 is turned into 198.530.980.148 by AWK (extra zeroes), or 72.14.199.7 into 72.140.1990.7.

Any help or insight much appreciated.

Thanks Paul

UPDATE

Just to mention Ed's very helpful alternate script below:

awk -v OFS=, '{cnt[$1]++} END{for (x in cnt) print x, cnt[x]}' /var/log/varnish/varnishncsa.log

It's MUCH faster than mine.

I left the answer as is though, as technically my incorrect syntax was the error.

Upvotes: 1

Views: 226

Answers (2)

Ed Morton
Ed Morton

Reputation: 203254

Your whole command line can be reduced to just this:

awk -v OFS=, '{cnt[$1]++} END{for (x in cnt) print x, cnt[x]}' /var/log/varnish/varnishncsa.log

Upvotes: 4

anubhava
anubhava

Reputation: 784998

Well I don't know your input file but this awk command:

awk -v q=, "/$x/ { count++ } END { print $x q count }"

is definitely not correct. You need to pass $x using same -v option you have for passing variable q.

Try this awk command:

awk -v x="$x" -v q="," '$0 ~ x { count++ } END { print x q count }'

Most likely all of your script can be written in one single awk command and there is no need of unnecessary cat, awk, uniq etc.

Upvotes: 2

Related Questions