k3090
k3090

Reputation: 25

Calculating the sum and average

My input file is price list of items, in that particular items, I want to calculate the sum and average of the rice items using awk?

Chenna Black |1|76.00|Chenna
Chenna White big|1|102.00|Chenna
Chenna White small|1|82.00|Chenna
Dhal-Gram|1|78.00|Dhal
Dhal-Moong|1|76.00|Dhal
Dhal-Orid|1|72.00|Dhal
Dhal-Toor|1|68.00|Dhal
Dhal-Green gram|1|88.00|Dhal
Rajma-Black|1|72.00|Rajma
Rajma-Pink|1|80.00|Rajma
Rice-Basmanthi|1|80.00|Rice
Rice-Boiled|1|42.00|Rice
Rice-Idly|1|33.00|Rice
Rice-IGBasamanthi|1|99.00|Rice
Rice-Karanatka Ponni|1|38.00|Rice
Rice-Nirapara Silky|1|235.00|Rice
Rice-Raw|1|40.00|Rice
Rice-Roasted Gram|1|100.00|Rice

Upvotes: 0

Views: 137

Answers (1)

Chris Seymour
Chris Seymour

Reputation: 85865

Sum and average of all rice items using awk:

$ awk -F'|' '$4=="Rice"{s+=$3;c++}END{printf "sum: %.2f avg: %.2f\n",s,s/c}' file
sum: 667.00 avg: 83.38

Upvotes: 1

Related Questions