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