如何用awk提取iostat输出中read行的avgserv参数值
Got it, let's fix this for you. The issue with your previous commands is that they're either grabbing the header or not filtering strictly enough to only target the read line's avgserv value. Here are a couple of robust solutions depending on how your iostat -D hdisk0 output is structured:
Case 1: Your output has separate read/write sections (common on AIX)
If your iostat output looks something like this (with a read: header line followed by the data row):
hdisk0: % tm_act Kbps tps Kb_read Kb_wrtn 0.1 0.4 0.0 160 0 read: % tm_act Kbps tps AvgServ ... 0.0 0.4 0.0 0.8 ... write: % tm_act Kbps tps AvgServ ... 0.0 0.0 0.0 0.0 ...
Use this command to dynamically find the AvgServ column and extract only the read row's value:
iostat -D hdisk0 | awk ' BEGIN {col=0} /AvgServ/ {for(i=1;i<=NF;i++) if($i=="AvgServ") col=i} /^[[:space:]]+[0-9]/ && prev~/read:/ {print $col} {prev=$0} '
How this works:
- We first scan for the header line containing
AvgServto find which column it’s in (so you don’t have to hardcode column numbers if the output format changes). - We then check for data rows (lines starting with spaces followed by numbers) where the previous line was the
read:section header. - Finally, we print the value from the
AvgServcolumn for that matching row.
Case 2: Your output has read/write labeled directly in data rows
If your output lists read/write as a field in the data rows like this:
Device type tps avgserv hdisk0 read 2.5 0.8 hdisk0 write 1.2 1.5
This simpler command will work perfectly:
iostat -D hdisk0 | awk '$2=="read" {print $4}'
Just adjust $2 and $4 to match the actual positions of the read label and avgserv value in your output.
Quick test tip
Run iostat -D hdisk0 manually first to check your exact output format—this will help you pick the right command or tweak column numbers if needed.
内容的提问来源于stack exchange,提问作者edwio

