Need help to parse iostat command output

Hi,

I got the code below is one of the threads from this forum.

lineCount=$(iostat | wc -l)
numDevices=$(expr $lineCount - 7);

iostat $interval -x -t |
awk -v awkCpuFile=$cpuFile -v awkDeviceFile=$deviceFile -v awkNumDevices=$numDevices '
BEGIN {
	print "date/time,%user,%nice,%system,%iowait,%steal,%idle" > "awkCpuFile"
	print "date/time,device,rrqm/s,wrqm/s,r/s,w/s,rsec/s,wsec/s,avgrq-sz,avgqu-sz,await,svctm,%util" > "awkDeviceFile"
}
NF==2{
	s=$0
	getline; getline; $1=$1
	gsub(/ /,",",$0)
	print s "," $0 > "awkCpuFile"	
}
/Device:/{
	for (i=0; i<awkNumDevices; i++) {
		getline; $1=$1
		gsub(/ /,",",$0)
		print s "," $0 > "awkDeviceFile"
	}
}'

Once this script is executed,
I am able to see the device statistics in the output file "awkDeviceFile" but there is no value displayed in the CPU statistics file "awkCpuFile"

Could you please let me know what could be the reason for the same?

Please always post the entire script - in your above snippet, several variables are unset and thus could negatively influence the script's execution.

Amongst which is $cpuFile - if that is empty, no file is produced. Other reasons might be

  • NF != 2, so that action is not executed and no cpu values printed..
  • All print redirections happen to string constants, not the variables defined, so the produced files' names may not match your expectations.

Dear RudiC,

Thanks for pointing out the possibilities in quick time.
The script works fine now with only one change:
Instead of NF==2, I used NF==3 and now am able to print both cpu statistics as well as Device statistics

Best Regards,
Gopi