0

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 ?

1
  • column 7? You're adding the days of the month?? Commented Nov 6, 2014 at 14:42

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I have made some changes to your code, feel free to roll back the edit if you like. Hopefully the edit summary is self-explanatory :)
It's not strictly necessary to initialize variables to zero.
2

Assuming you're doing a directory listing of the current directory, you really shouldn't parse the output of ls. Here are 2 alternatives:

stat -c "%s" * | awk '{sum+=$1} END {print sum}'
perl -E '-f $_ and $sum += -s _ for glob("*"); say $sum'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.