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.


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.

  • This is not clear. Line with 238 is not line 1 but line 2. Better provide an expected sample output – Gilles Quenot 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

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
  • 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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