Add Column at end which is average of row values

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Add Column at end which is average of row values

OMEGA
I have a file named product_sale With Below format product_name,jan,feb,march cakes,20,40,60 pizzas,10,4,6 cokes,10,20,40 I want to add extra column which is average value of columns from jan to march. Like below format. product_name,jan,feb,march,Average_sale cakes,20,40,60,40 pizzas,11,4,6,7 cokes,10,10,40,20 How to generate this ?
Reply | Threaded
Open this post in threaded view
|

Re: Add Column at end which is average of row values

Guru
Administrator
$ cat file
product_name,jan,feb,march
cakes,20,40,60
pizzas,10,4,6
cokes,10,20,40
$
$ awk -F, 'NR!=1{$5=($2+$3+$4)/3}NR==1{$5="Average"}1' OFS=, file
product_name,jan,feb,march ,Average
cakes,20,40,60 ,40
pizzas,10,4,6 ,6.66667
cokes,10,20,40,23.3333
$