awk getline to fetch output

Input File:

CPU minf mjf xcal  intr ithr  csw icsw migr smtx  srw syscl  usr sys  wt idl
  0   37   0  594  2205 1174  123    8    6   36    0  1954    6   8   0  86
  1    1   0   49    26    0   44    3    2    6    0   624    2   1   0  97
  2    0   0    5     5    0    8    1    0    1    0    89    0   0   0 100
  3    0   0    0     2    0    2    1    0    0    0    13    0   0   0 100
  4    5   0   46    18    0   33    3    2   13    0   486    2   1   0  98
  5    0   0    1     2    0    2    1    0    1    0    28    0   0   0 100
CPU minf mjf xcal  intr ithr  csw icsw migr smtx  srw syscl  usr sys  wt idl
  0   37   0  594  2205 1174  123    8    6   36    0  1954    6   8   0  86
  1    1   0   49    26    0   44    3    2    6    0   624    2   1   0  97
  2    0   0    5     5    0    8    1    0    1    0    89    0   0   0 100
  3    0   0    0     2    0    2    1    0    0    0    13    0   0   0 100
  4    5   0   46    18    0   33    3    2   13    0   486    2   1   0  98
  5    0   0    1     2    0    2    1    0    1    0    28    0   0   0 100
CPU minf mjf xcal  intr ithr  csw icsw migr smtx  srw syscl  usr sys  wt idl
  0   37   0  594  2205 1174  123    8    6   36    0  1954    6   8   0  86
  1    1   0   49    26    0   44    3    2    6    0   624    2   1   0  97
  2    0   0    5     5    0    8    1    0    1    0    89    0   0   0 100
  3    0   0    0     2    0    2    1    0    0    0    13    0   0   0 100
  4    5   0   46    18    0   33    3    2   13    0   486    2   1   0  98
  5    0   0    1     2    0    2    1    0    1    0    28    0   0   0 100

I am adding all the values in last column of CPU0 and then sending the output to C0.
For this I am using the code below:

cpu0()
{
cat $1|awk 'BEGIN{i=1;sum=0;}
/CPU/{getline;cpu0=$NF;sum=sum+cpu0;i=i+1;}
END {j=1;k=1;OFS="\t"
while ( j < i)
{
printf "%s\n",cpu0[j]
j=j+1;
}
printf "sum is %s total entries %s\n",sum,i;
printf "avg is %s\n",sum/i;
}'
} # End of function

cpu1()
{
cat $1|awk 'BEGIN{i=1;sum=0;}
/CPU/{getline;getline;cpu1=$NF;sum=sum+cpu1;i=i+1;}
END {j=1;k=1;OFS="\t"
while ( j < i)
{
printf "%s\n",cpu1[j]
j=j+1;
}
printf "sum is %s total entries %s\n",sum,i;
printf "avg is %s\n",sum/i;
}'
} # End of function

cpu0 $1>C0
cpu1 $1>C1

But the concern is number of CPU�s is not fixed and I was trying to implement a for loop:
cpu()
{
arrcpu[1]=0
cat $1|awk 'BEGIN{i=1;sum=0;}
/CPU/{getline;arrcpu=$NF;sum=sum+arrcpu;i=i+1;}
END {j=1;k=1;OFS="\t"
while ( j < i)
{
printf "%s\n",arrcpu[j]
j=j+1;
}
printf "sum is %s total entries %s\n",sum,i;
printf "avg is %s\n",sum/i;
}'
} # End of function

for num in `cat cpu_num`
do
cpu $1 > C$num
done

However I am not able to increment �getline� every time the loop is executed because of which same output is returned every time. Please suggest if there is any other alternative to this problem.

To keep the forums high quality for all users, please take the time to format your posts correctly.

First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags

```text
 and 
```

by hand.)

Second, avoid adding color or different fonts and font size to your posts. Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.

Third, be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums

---------- Post updated at 02:29 PM ---------- Previous update was at 02:26 PM ----------

Could you please post an example of the desired output?

Thanks a lot.

The Desired Output files are as below:
C0
C1

Values in C0
86
86
86
sum is 258 total entries 3
avg is 86

Similarly for C1

Use gawk, nawk or /usr/xpg4/bin/awk on Solaris:

awk 'END {
  for (Cpu in cpu) {
    outfile = "C" Cpu
    printf "Values in C%s\n%s\n", Cpu, cpu[Cpu] > outfile
    printf "Sum is %d total entries %d\n", \
    sum[Cpu], count[Cpu] > outfile 
    printf "Avg is %s\n", \
    sum[Cpu] ? sum[Cpu] / count[Cpu] : "N/A" > outfile
    close(outfile)
    }
  }    
/^ *[0-9]/ {
  cpu[$1] = cpu[$1] ? cpu[$1] RS $NF : $NF
  sum[$1] += $NF; count[$1]++
  }' infile

---------- Post updated at 03:21 PM ---------- Previous update was at 02:57 PM ----------

Actually it should be 0 instead of "N/A".