hi i need help in my script
i have a file
a.txt
3,4,5,13
6,7,8,45
9,0,1,67
i want to add 2nd and 3rd colum like o/p will be by adding all values of colum2(4+7+0) and colum 3(5+8+11)
o/p:
colum 2: 11
colum 3: 14
hi i need help in my script
i have a file
a.txt
3,4,5,13
6,7,8,45
9,0,1,67
i want to add 2nd and 3rd colum like o/p will be by adding all values of colum2(4+7+0) and colum 3(5+8+11)
o/p:
colum 2: 11
colum 3: 14
awk -F"," '{col2+=$2;col3+=$3}END{print "column 2: "col2;print "column 3: "col3}' file
Thanks for quick reply....
can u pls suggest me command if i will be having more than 2 coloums like
2,3,4,5,6,7,8,9,0,33,44,55,66
3,4,5,6,7,8,3,2,1,55,66,78,88
and if i have to add coloums from 2 nd to 9.
can we use for loop here to make it variable if requirement is for more than 10 coloums
awk '{ for (i=1; i<=NF; ++i) sum += $i; if (i > max) max=i }
END { s=""; for (i=1; i<=max; ++i) { printf "%s%s", s, sum; s=OFS; } printf "\n" }' a.txt
Hi thanks for reply but when i am running this command it gives o/p
>cat a.txt
2,3,4,5,6,7,8,9,0,33,44,55,66
3,4,5,6,7,8,3,2,1,55,66,78,88
>awk '{ for (i=1; i<=5; ++i) sum [i]+= $i; if (i > max) max=i } END { s=""; for (i=1; i<=max; i++) { printf "%s%s", s, sum[i]; s=OFS; } printf "\n" }' a.txt
0 0 0 0 0
it not adding the coloums
You need to specify the field separator -F, -- sorry for missing that. Maybe you also want s=IFS rather than s=OFS (or maybe not).
Thanks era...........