BEGIN { # 変数の初期化 FS="," dog_sum1 = 0 dog_sum2 = 0 cat_sum1 = 0 cat_sum2 = 0 } { if ($2 == "dog") { dog_sum1 += $3 dog_sum2 += $4 } else if ($2 == "cat") { cat_sum1 += $3 cat_sum2 += $4 } } END { # 結果を表示する print "category,sum1,sum2" print "dog,"dog_sum1","dog_sum2 print "cat,"cat_sum1","cat_sum2 }test.csv
This is test. 1,dog,10,20 2,cat,500,200 3,dog,40,20 4,cat,10,500実行結果
> awk -f test04-3.awk test.csv category,sum1,sum2 dog,50,40 cat,510,700
0 件のコメント:
コメントを投稿