我的输入文件是商品的价目表,在该特定商品中,我想使用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
答案 0 :(得分:1)
使用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