TOP IO DISK Utilization scripting

Hi Fiends,

I am new to scripting., I want to calculate the 2nd column in the below output and print the average for each hdisk.

Below is the output of sar command,

hdisk0 0
hdisk0 2
hdisk0 0
hdisk1 2
hdisk1 2
hdisk1 2
hdisk2 1
hdisk2 0
hdisk2 0

Thanks,
Srinivasan

You could try something like:

awk '{	c[$1]++; s[$1] += $2}
END {	for(i in c) print i, s / c}' sar.out

If you're using a Solaris/SunOS system, use /usr/xpg4/bin/awk , /usr/xpg6/bin/awk , or nawk instead of awk .

Don,

Thanks for u reply.

I am getting the following error.

cat /tmp/sar.txt | grep -v -p Average |egrep "hdisk" |cut -c 14-26|sort -rn
hdisk1      2
hdisk1      0
hdisk1      0
hdisk0      2
hdisk0      0
hdisk0      0
 
A=`cat /tmp/sar.txt | grep -v -p Average |egrep "hdisk" |cut -c 14-26|sort -rn`
 
awk '{ c[$1]++; s[$1] += $2}
END { for(i in c) print i, s / c }' $A
awk: 0602-533 Cannot find or open file hdisk1.
 The source line number is 2.

---------- Post updated at 02:55 AM ---------- Previous update was at 01:42 AM ----------

Hi friends,

I got the script from the earlier post.

awk '{
> c[$1]=1
> for(i=2;i<=NF;i++)
> {
>  total[$1,i]+=$i
>  count[$1,i]++
> }
> }
> END{
> for(i in c)
> {
>  printf "%s",i
>  for(j=2;count[i,j];j++)
>   printf "\t%.1f", (total[i,j]/count[i,j])
>  printf "\n"
> }
> }' sar.txt

A=`cat /tmp/sar.txt | grep -v -p Average |egrep "hdisk" |cut -c 14-26|sort -rn`
could be change to some more modern
A=$(cat /tmp/sar.txt | grep -v -p Average |egrep "hdisk" |cut -c 14-26|sort -rn)

You can not add variable to awk the way you try to do it.

correct

awk '{ c[$1]++; s[$1] += $2}
END { for(i in c) print i, s / c }' <<< "$A"

or

echo "$A" | awk '{ c[$1]++; s[$1] += $2}
END { for(i in c) print i, s / c }'

You also need to have $A in quotes like this "$A" to preserve the multiple lines.

If you give us some lines of sar.txt, we could for sure shorten the code for $A