zeek/aux/broctl/bin/helpers/to-bytes.awk
Robin Sommer 2b6ad76bd5 Creating a branch release/1.5 with the current 1.5.3 release code.
This is so that people working from the current stable version can
still start using git.
2011-03-09 15:26:01 -08:00

23 lines
914 B
Awk

# $Id: to-bytes.awk 6811 2009-07-06 20:41:10Z robin $
# Converts strings such as 12K, 42M, etc. into bytes.
# If def_factor is set, it's applied to values without any unit.
BEGIN {
if ( def_factor == 0 )
def_factor = 1;
}
{
for ( i = 1; i <= NF; i++) {
if ( match($i, "^(-?[0-9.]+)By?$") ){ $i = substr($i, RSTART, RLENGTH-1); }
else if ( match($i, "^(-?[0-9.]+)Ki?$") ){ $i = substr($i, RSTART, RLENGTH-1) * 1024; }
else if ( match($i, "^(-?[0-9.]+)Mi?$") ){ $i = substr($i, RSTART, RLENGTH-1) * 1024 * 1024; }
else if ( match($i, "^(-?[0-9.]+)Gi?$") ){ $i = substr($i, RSTART, RLENGTH-1) * 1024 * 1024 * 1024; }
else if ( match($i, "^(-?[0-9.]+)Te?$") ){ $i = substr($i, RSTART, RLENGTH-1) * 1024 * 1024 * 1024 * 1024; }
else if ( match($i, "^(-?[0-9.]+)$") ) { $i = substr($i, RSTART, RLENGTH) * def_factor; }
printf("%s ", $i);
}
print "";
}