2

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.

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.

2
  • This is not clear. Line with 238 is not line 1 but line 2. Better provide an expected sample output Commented Feb 27, 2013 at 3:36
  • Count and Process are labels. So for Count column the first line is 238 and for Process its 0.
    – dimas
    Commented Feb 27, 2013 at 3:39

1 Answer 1

3

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
2
  • 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
    Commented Feb 27, 2013 at 4:14
  • Thanks alot, I edited my post above and got that right result using your answer.
    – dimas
    Commented Feb 27, 2013 at 4:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .