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.