Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have this output:

  Count Process
    238 0
     1 1124027
     1 1124065
     1 1124066
     1 1124069
     1 1124072
     1 1124073
     1 1547365
     1 1547366

I wanted to add all Count except for the first line (238). How do I do this on awk?

so far I have this
awk 'NF == 1 {for (i=2;i<=FNR;++i) sum += i print $i}'
but its an error.

Any help is greatly appreciated. Thanks

Edit

I am trying to parse the output of this command:

cat /proc/8920/maps | awk '{print$5}' | sort | uniq -c |`awk 'NF == 1 {for (i=2;i<=FNR;++i) sum += i print $i}'` 

Everything works fine until uniq -c, but after that I have problem with awk script to add all lines in first columns except for the first line which is 238. I hope this clears things up.

Hauke Laging's answer and I came up with this script

cat /proc/8920/maps | awk '{print$5}' | sort | uniq -c | awk 'NR>1 { sum+= $1} END  {print sum}'

now its working. Thanks very much!

share|improve this question
    
This is not clear. Line with 238 is not line 1 but line 2. Better provide an expected sample output –  sputnick Feb 27 '13 at 3:36
    
Count and Process are labels. So for Count column the first line is 238 and for Process its 0. –  dimas Feb 27 '13 at 3:39
add comment

1 Answer 1

up vote 2 down vote accepted

It doesn't make sense to add a value only if it is 1. In that case you can count the matching lines instead:

grep -c " 1 " < inputfile

Real adding is done this way:

awk 'NR>1 { sum+=$1 }; END { print sum }' inputfile
share|improve this answer
    
actually that's just a sample output. The real output is much longer than that and the first column have other values. I'll edit my post so that it would be clearer. –  dimas Feb 27 '13 at 4:14
    
Thanks alot, I edited my post above and got that right result using your answer. –  dimas Feb 27 '13 at 4:23
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.