The so-far expertly crafted script
iostat -p -x 1 2| grep $1[^[:digit:]] | awk '{print $9}'
will return two lines:
0.16
0.00
because iostat is taking two samples. But you already knew that. My question is since you and I both know it will be exactly two lines every time, how I pick out exactly the second?
Add
NR==2before{print $9}to tellawkto match record 2.For example, on my system,
produced
where that “0.00” line is the result of the third command.
Additional note: It isn’t necessary to run a separate
grepcommand, asawkcan match text. However, the awk script then needs an index variable. The first of the following examples is a simplegrepto show whatawkworks on. The next two examples are straightforwardawkcode that has a match test to increment an index and another match test plus index test to print. The last example shows how to get rid of extra appearances of the match expression. Note, in these examples I replacedprint $9withprintfor clarity, and$1[^[:digit:]]withsda[^0-9]. Here are the examples which do the matching inawkinstead of ingrep: