I have a strange AWK behaviour: a string inside an AWK script is converted to integer without a problem, but the same string - when received from outside, does not convert correctly to integer.
For instance, when the string total_bytes
is received from within the script:
$ cat sample.awk
END {
total_bytes = "113402391" ;
#total_bytes = $8
total_bits = (total_bytes) * 8 ;
print "Total transmitted bytes:", total_bytes ;
print "Total transmitted bits:", total_bits ;
}
$ cat ./one_liner | awk -F, -f sample.awk
Total transmitted bytes: 113402391
Total transmitted bits: 907219128
$
The total_bits
variable is calculated correctly.
However, if I try to do the same using input from outside the AWK script:
$ cat sample.awk
END {
#total_bytes = "113402391" ;
total_bytes = $8
total_bits = (total_bytes) * 8 ;
print "Total transmitted bytes:", total_bytes ;
print "Total transmitted bits:", total_bits ;
}
$ cat one_liner
"117403","885.406632000","192.168.0.25","192.168.0.26","TCP","60","8080 > 61165 [ACK] Seq=107051610 Ack=134 Win=262144 Len=0","113402391","2014-08-06 12:50:50.564785000"
$ cat ./one_liner | awk -F, -f sample.awk
Total transmitted bytes: "113402391"
Total transmitted bits: 0
$
The total_bits
variable is calculated incorrectly - as 0.
Environment:
OS: CYGWIN_NT-6.1-WOW64 1.7.29(0.272/5/3) i686 Cygwin
AWK: GNU Awk 4.1.1, API: 1.1 (GNU MPFR 3.1.2, GNU MP 6.0.0)
the cause is, your $8
is "xxxx"
with quotes!
so what awk does is 8* "12334"
, it would be 0
kent$ awk 'BEGIN{print (8 * "\"10\"")}'
0
the quick solution is removing "
from your $8
before going into the calculation.