Reputation: 9
I have create a one liner that will sum up each row in a csv file, from the second entry onwards. But I want to find the highest number from the sum of rows
Example file output: There are thousands of rows
03/Mar/2016:00:14,19772,7494,11293,9467
03/Mar/2016:00:15,18041,13241,9715,8968
03/Mar/2016:00:16,17441,13534,9926,9301
03/Mar/2016:00:17,17709,14243,9022,9209
03/Mar/2016:00:18,16368,13535,8761,8313
03/Mar/2016:00:19,17074,13224,8868,7789
03/Mar/2016:00:20,16783,13666,9499,8763
03/Mar/2016:00:21,16665,12962,8821,8862
Example script: This is what I have achieved by calculating each row but need to just find the highest number from the calculated rows. Any ideas?
awk 'BEGIN {FS=OFS=","} {sum=0; for(i=2;i<=NF;i++) {sum+=$i}; print $0,"sum:"sum,}' /tmp/101.20160304.csv
cheers
Upvotes: 0
Views: 365
Reputation: 246847
awk is quite capable of remembering a maximum value.
awk -F, '
# for every row, calculate the sum
{sum = 0; for (i=2; i<=NF; i++) sum += $i}
# set the max value (if the first row, initialize the max value)
NR == 1 || sum > max {max = sum}
END {print max}
' file
For your sample data, this is the max:
50202
Upvotes: 2
Reputation: 195079
you can pipe your awk output to :
awk_output|sort -t':' -nrk4|head -1
this does sort by the sum descending, then pick the first row. Of course you can re-write your awk, to do this in one shot.
Upvotes: 1