awk to sum the numbers(floating) and group it on a unique key
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
I am trying to sum up the numbers and grouping it on a unique key but the sum output is sometimes shows as exponential and some time it shows as integer/floating after rounding off.
Input File
Id|LegNum|Amount1|Amount2|Location
123456|0|238157.5|4280338.799|CL
123456|1|8487.924693|2541829.5|CL
123456|1|14516.47036|31881.6|CL
123789|0|483773.787|10410.78659|CL
456789|0|1321034|1057203.46|NY
456789|1|65802|4913.79677|NY
567890|0|683651.0865|533.617279|IN
567890|0|705864.844|907210|IN
using below awk to sum and group it at first and second column
awk 'BEGIN "; OFS = FS; NR==1 print $1,$2,$3,$4,$5;next"$2]+=$4;c[$1"ENDfor(i in a)print i,a[i],b[i],c[i]' InputFile.txt >> output.txt
output.txt shows the sums as exponential OR after rounding off as mentioned below.
Id|LegNum|Amount1|Amount2|Location
123789|0|483774|10410.8|CL
123456|0|238158|4.28034e+06|CL
567890|0|1.38952e+06|907744|IN
123456|1|23004.4|2.57371e+06|CL
456789|0|1321034|1.0572e+06|NY
456789|1|65802|4913.8|NY
But I want the output as floating (without rounding off) if the number is floating and as integer if the number is integer. and definitely don't want the exponential output.
shell awk
add a comment |Â
up vote
0
down vote
favorite
I am trying to sum up the numbers and grouping it on a unique key but the sum output is sometimes shows as exponential and some time it shows as integer/floating after rounding off.
Input File
Id|LegNum|Amount1|Amount2|Location
123456|0|238157.5|4280338.799|CL
123456|1|8487.924693|2541829.5|CL
123456|1|14516.47036|31881.6|CL
123789|0|483773.787|10410.78659|CL
456789|0|1321034|1057203.46|NY
456789|1|65802|4913.79677|NY
567890|0|683651.0865|533.617279|IN
567890|0|705864.844|907210|IN
using below awk to sum and group it at first and second column
awk 'BEGIN "; OFS = FS; NR==1 print $1,$2,$3,$4,$5;next"$2]+=$4;c[$1"ENDfor(i in a)print i,a[i],b[i],c[i]' InputFile.txt >> output.txt
output.txt shows the sums as exponential OR after rounding off as mentioned below.
Id|LegNum|Amount1|Amount2|Location
123789|0|483774|10410.8|CL
123456|0|238158|4.28034e+06|CL
567890|0|1.38952e+06|907744|IN
123456|1|23004.4|2.57371e+06|CL
456789|0|1321034|1.0572e+06|NY
456789|1|65802|4913.8|NY
But I want the output as floating (without rounding off) if the number is floating and as integer if the number is integer. and definitely don't want the exponential output.
shell awk
You should be able to useprintf
with%f
to print floating point.
â unxnut
Jul 5 at 9:21
but at which place, I should use it. i tried to print using for(i in a)printf "%fn" i,a[i],b[i],c[i] but didn't work as expected
â Amit Singh
Jul 5 at 9:34
@AmitSingh If you are usingprintf()
, you will have to specify the format for each individual parameter.
â Kusalananda
Jul 5 at 9:42
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I am trying to sum up the numbers and grouping it on a unique key but the sum output is sometimes shows as exponential and some time it shows as integer/floating after rounding off.
Input File
Id|LegNum|Amount1|Amount2|Location
123456|0|238157.5|4280338.799|CL
123456|1|8487.924693|2541829.5|CL
123456|1|14516.47036|31881.6|CL
123789|0|483773.787|10410.78659|CL
456789|0|1321034|1057203.46|NY
456789|1|65802|4913.79677|NY
567890|0|683651.0865|533.617279|IN
567890|0|705864.844|907210|IN
using below awk to sum and group it at first and second column
awk 'BEGIN "; OFS = FS; NR==1 print $1,$2,$3,$4,$5;next"$2]+=$4;c[$1"ENDfor(i in a)print i,a[i],b[i],c[i]' InputFile.txt >> output.txt
output.txt shows the sums as exponential OR after rounding off as mentioned below.
Id|LegNum|Amount1|Amount2|Location
123789|0|483774|10410.8|CL
123456|0|238158|4.28034e+06|CL
567890|0|1.38952e+06|907744|IN
123456|1|23004.4|2.57371e+06|CL
456789|0|1321034|1.0572e+06|NY
456789|1|65802|4913.8|NY
But I want the output as floating (without rounding off) if the number is floating and as integer if the number is integer. and definitely don't want the exponential output.
shell awk
I am trying to sum up the numbers and grouping it on a unique key but the sum output is sometimes shows as exponential and some time it shows as integer/floating after rounding off.
Input File
Id|LegNum|Amount1|Amount2|Location
123456|0|238157.5|4280338.799|CL
123456|1|8487.924693|2541829.5|CL
123456|1|14516.47036|31881.6|CL
123789|0|483773.787|10410.78659|CL
456789|0|1321034|1057203.46|NY
456789|1|65802|4913.79677|NY
567890|0|683651.0865|533.617279|IN
567890|0|705864.844|907210|IN
using below awk to sum and group it at first and second column
awk 'BEGIN "; OFS = FS; NR==1 print $1,$2,$3,$4,$5;next"$2]+=$4;c[$1"ENDfor(i in a)print i,a[i],b[i],c[i]' InputFile.txt >> output.txt
output.txt shows the sums as exponential OR after rounding off as mentioned below.
Id|LegNum|Amount1|Amount2|Location
123789|0|483774|10410.8|CL
123456|0|238158|4.28034e+06|CL
567890|0|1.38952e+06|907744|IN
123456|1|23004.4|2.57371e+06|CL
456789|0|1321034|1.0572e+06|NY
456789|1|65802|4913.8|NY
But I want the output as floating (without rounding off) if the number is floating and as integer if the number is integer. and definitely don't want the exponential output.
shell awk
asked Jul 5 at 8:30
Amit Singh
51
51
You should be able to useprintf
with%f
to print floating point.
â unxnut
Jul 5 at 9:21
but at which place, I should use it. i tried to print using for(i in a)printf "%fn" i,a[i],b[i],c[i] but didn't work as expected
â Amit Singh
Jul 5 at 9:34
@AmitSingh If you are usingprintf()
, you will have to specify the format for each individual parameter.
â Kusalananda
Jul 5 at 9:42
add a comment |Â
You should be able to useprintf
with%f
to print floating point.
â unxnut
Jul 5 at 9:21
but at which place, I should use it. i tried to print using for(i in a)printf "%fn" i,a[i],b[i],c[i] but didn't work as expected
â Amit Singh
Jul 5 at 9:34
@AmitSingh If you are usingprintf()
, you will have to specify the format for each individual parameter.
â Kusalananda
Jul 5 at 9:42
You should be able to use
printf
with %f
to print floating point.â unxnut
Jul 5 at 9:21
You should be able to use
printf
with %f
to print floating point.â unxnut
Jul 5 at 9:21
but at which place, I should use it. i tried to print using for(i in a)printf "%fn" i,a[i],b[i],c[i] but didn't work as expected
â Amit Singh
Jul 5 at 9:34
but at which place, I should use it. i tried to print using for(i in a)printf "%fn" i,a[i],b[i],c[i] but didn't work as expected
â Amit Singh
Jul 5 at 9:34
@AmitSingh If you are using
printf()
, you will have to specify the format for each individual parameter.â Kusalananda
Jul 5 at 9:42
@AmitSingh If you are using
printf()
, you will have to specify the format for each individual parameter.â Kusalananda
Jul 5 at 9:42
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
awk
uses its OFMT
variable as the default printf
format for outputting numbers. This is %.6g
by default.
You may set this to something like %f
or to a more precise format appropriate for your data, or you may want to use printf()
instead of print
to format the output explicitly.
Example:
$ awk 'BEGIN print 100000000.10, 2, "hello" '
1e+08 2 hello
$ awk -vOFMT='%f' 'BEGIN print 100000000.10, 2, "hello" '
100000000.100000 2 hello
$ awk -vOFMT='%.2f' 'BEGIN print 100000000.10, 2, "hello" '
100000000.10 2 hello
$ awk 'BEGIN printf("%.2f %d %sn", 100000000.10, 2, "hello") '
100000000.10 2 hello
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
awk
uses its OFMT
variable as the default printf
format for outputting numbers. This is %.6g
by default.
You may set this to something like %f
or to a more precise format appropriate for your data, or you may want to use printf()
instead of print
to format the output explicitly.
Example:
$ awk 'BEGIN print 100000000.10, 2, "hello" '
1e+08 2 hello
$ awk -vOFMT='%f' 'BEGIN print 100000000.10, 2, "hello" '
100000000.100000 2 hello
$ awk -vOFMT='%.2f' 'BEGIN print 100000000.10, 2, "hello" '
100000000.10 2 hello
$ awk 'BEGIN printf("%.2f %d %sn", 100000000.10, 2, "hello") '
100000000.10 2 hello
add a comment |Â
up vote
2
down vote
accepted
awk
uses its OFMT
variable as the default printf
format for outputting numbers. This is %.6g
by default.
You may set this to something like %f
or to a more precise format appropriate for your data, or you may want to use printf()
instead of print
to format the output explicitly.
Example:
$ awk 'BEGIN print 100000000.10, 2, "hello" '
1e+08 2 hello
$ awk -vOFMT='%f' 'BEGIN print 100000000.10, 2, "hello" '
100000000.100000 2 hello
$ awk -vOFMT='%.2f' 'BEGIN print 100000000.10, 2, "hello" '
100000000.10 2 hello
$ awk 'BEGIN printf("%.2f %d %sn", 100000000.10, 2, "hello") '
100000000.10 2 hello
add a comment |Â
up vote
2
down vote
accepted
up vote
2
down vote
accepted
awk
uses its OFMT
variable as the default printf
format for outputting numbers. This is %.6g
by default.
You may set this to something like %f
or to a more precise format appropriate for your data, or you may want to use printf()
instead of print
to format the output explicitly.
Example:
$ awk 'BEGIN print 100000000.10, 2, "hello" '
1e+08 2 hello
$ awk -vOFMT='%f' 'BEGIN print 100000000.10, 2, "hello" '
100000000.100000 2 hello
$ awk -vOFMT='%.2f' 'BEGIN print 100000000.10, 2, "hello" '
100000000.10 2 hello
$ awk 'BEGIN printf("%.2f %d %sn", 100000000.10, 2, "hello") '
100000000.10 2 hello
awk
uses its OFMT
variable as the default printf
format for outputting numbers. This is %.6g
by default.
You may set this to something like %f
or to a more precise format appropriate for your data, or you may want to use printf()
instead of print
to format the output explicitly.
Example:
$ awk 'BEGIN print 100000000.10, 2, "hello" '
1e+08 2 hello
$ awk -vOFMT='%f' 'BEGIN print 100000000.10, 2, "hello" '
100000000.100000 2 hello
$ awk -vOFMT='%.2f' 'BEGIN print 100000000.10, 2, "hello" '
100000000.10 2 hello
$ awk 'BEGIN printf("%.2f %d %sn", 100000000.10, 2, "hello") '
100000000.10 2 hello
edited Jul 5 at 9:57
answered Jul 5 at 9:38
Kusalananda
101k13199312
101k13199312
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f453551%2fawk-to-sum-the-numbersfloating-and-group-it-on-a-unique-key%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
You should be able to use
printf
with%f
to print floating point.â unxnut
Jul 5 at 9:21
but at which place, I should use it. i tried to print using for(i in a)printf "%fn" i,a[i],b[i],c[i] but didn't work as expected
â Amit Singh
Jul 5 at 9:34
@AmitSingh If you are using
printf()
, you will have to specify the format for each individual parameter.â Kusalananda
Jul 5 at 9:42