I am writing a shell script to check the vmstat si
and so
data at various time intervals
vmstat 1
sample output:
procs -----------memory---------- ---swap-- -----io---- -system-- ----cpu----
r b swpd free buff cache si so bi bo in cs us sy id wa
0 0 45820 899252 86700 468520 0 0 0 60 127 5821 20 7 34 0
0 0 45820 899252 86704 468504 0 0 0 32 44 104 0 0 100 0
I want to use awk, sed to extract si
and so
into different variables for further usage. I Am new to awk, sed and am still struggling to find my way through. Can you show me how I can do this ?
This AWK script reads the second line and uses the field headers as indices into the data on each line so you can refer to them by name. Here is the one-liner. I break it out line by line below.
As shown, it prints the contents of the "si" column and the average at the end. You could handle multiple fields or iterate over all the fields.
You can expand on this to handle other fields, do comparisons of one line to the previous one or do totals or other calculations. You can stop, as I have shown, after a certain number of records.
The
-n
option tovmstat
causes the header to only be printed once.The breakdown:
or more "standard" way:
NF
variable gives you the number of fields in the current linegetline
reads the next input lineI would feel remiss if I didn't supply the perl oneliner equivalents: