Hi
My Requirement is to take the sum of each column
below is the input file.
1 2 3 4
1 2 3 4
1 2 3 4
Initial i was using below command to achieve my desired result. however this was adding the row and not column.
i am not able understand why this is happening
awk '{for(i=1;i<=NF;i++)x+=$i;print x;x=0}' M.txt ----->1
After many hit&trial attempt and googling i am able to sum up the column . which is mentioned below.
awk '{for(i=1;i<=NF;i++) sum+=$i;};END {for (k=1;k<=NF;k++) print sum[k];}' t.txt ------->2
Also not able to understand why using this bracket
[] is giving correct solution.
although i have achieved my target but not able to understand the detail working of the command.
as in both the command i am using NF but still in command 1 it sum up the row and not column.
scriptor