Q:

How to calculate sum inside awk command in unix?

My newProcessFile.txt looks like this 
total 16
-rw-rw-r--    1 jaind    staff             0 Jul 14 08:54 cdim_landing_addl_addr.out
-rw-rw-r--    1 jaind    staff             0 Jul 17 06:13 cdim_merge_alert_log.out
-rw-rw-r--    1 jaind    staff             0 Jul 11 09:02 merge_log.out
-rw-rw-r--    1 jaind    staff             0 Nov 06 07:12 newProcessFile.txt
-rw-------    1 jaind    staff           969 Aug 13 02:22 nohup.out
-rw-r-----    1 jaind    staff           474 Apr 28 2014  profile_old
drwxr-x---    2 jaind    staff           256 Nov 06 04:45 test_Chiranjib

Whenever I am trying to compute the sum at column numbered 7 it prints the outputs as 0.
awk 'BEGIN{ FS = ","; sum = 0; count = 0 } {if(NR >= 2){sum = sum + $7; count ++; print $1,$2,$5,sum}}' newProcessFile.txt

Sum is not being computed . What am I missing ?

A:

You need to remove the field separator from your awk command as you are not using "," to separate fields.
awk 'NR >= 2{sum += $7; print $1,$2,$5,sum}' newProcessFile.txt

I have removed the counter variable as well as it was not used anywhere.

