0

I am currently printing out values from vmstat stress tests using this:

  vmstat -a $INT $CNT | awk 'NR > 1 {print $13,$14,$15}'> vm.log_$D & sleep 5 && stress --vm $cores -t $seconds_to_run 

I think it might be tidier to print them out in arrays but when I do, I always get a syntax error of { missing even when there isn't. How can I convert this to print the values of column $13, $14, $15 through using arrays rather than the way above?

The output of both should be the same but I feel the arrays look tidier in terms of output.

iostat output

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
          17.74    1.77   31.43   25.05    0.00   24.02

Device:            tps    kB_read/s    kB_wrtn/s    kB_read    kB_wrtn
sda              51.97      1349.36        32.86     392947       9568

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           9.18   78.57   12.24    0.00    0.00    0.00

Device:            tps    kB_read/s    kB_wrtn/s    kB_read    kB_wrtn
sda               2.04        12.24         0.00         12          0

I would like for when I run it for only the percentage columns to be printed, not the kb read ones.

EDIT**********************************

sar -D output-I would like a suitable command to run alongside stress -hdd but this is the output which sar -D 1 10 gives.

    4296roryhbmc
    1944    rootal,
    3361    root
Tasks:     326total,
Cpu(s):25.7%us,54.1%sy,
Mem:2041916k
Swap:1046524kt    0 |   0     0 |   0     0 | 268   957 
 34  17  49   0   0   0|   0     0 |   0     0 |   0     0 | 279   679 
 49  31  21   0   0   0|   0     0 |   0     0 |   0     0 | 334  1874 
  4   6  90   0   0   0|   0     0 |   0     0 |   0     0 | 109   131 
 28  16  55   1   0   0|   0  4096B|   0     0 |   0     0 | 304   575 
 42  33  26   0   0   0|  20k  428k|   0     0 |   0     0 | 392  1343 
  4  10  86   0   0   0|   0     0 |   0     0 |   0     0 |  81   124 
  8  12  57  22   0   0|   0     0 |   0     0 |   0     0 | 113   193 
  2   9  66  22   0   0|   0     0 |   0     0 |   0     0 |  74   126 
  4  12  84   0   0   0|   0     0 |   0     0 |   0     0 |  92   140 
  8  21  54  17   0   0|   0     0 |   0     0 |   0     0 | 129   286 
  7  11  48  33   0  
*******************************
* Choose from the following: *
*******************************
* [1] Stress the CPU. *
* [2] Stress the Memory(RAM). *
* [3] Stress the disk drive. *
* [4] Stress the  hard disk drive. *
Press A to quit.
************************
Enter your menu choice [1-4]: 

How can I edit it so it will only show this?

$ sar -d

SunOS unknown 5.10 Generic_118822-23 sun4u    01/22/2006

00:00:01   device       %busy   avque   r+w/s  blks/s  avwait  avserv
. cut ...
14:00:02   dad0             31     0.6      78   16102     1.9     5.3
           dad0,c            0     0.0       0       0     0.0     0.0
           dad0,h           31     0.6      78   16102     1.9     5.3
           dad1              0     0.0       0       1     1.6     1.3
           dad1,a            0     0.0       0       1     1.6     1.3
           dad1,b            0     0.0       0       0     0.0     0.0
           dad1,c            0     0.0       0       0     0.0     0.0

source: http://www.ibm.com/developerworks/aix/library/au-unix-perfmonsar.html

  • 2
    Arrays of what? Bash arrays? Awk arrays? – muru Mar 1 '15 at 18:37
  • output in awk arrays – paul Mar 1 '15 at 18:44
  • 1
    And how would that be an improvement? – muru Mar 1 '15 at 18:45
  • Please explain/give an example of what you mean by "arrays look tidier in terms of output" - it's not clear what you're trying to do. – steeldriver Mar 1 '15 at 18:47
  • 1
    None at all. The internal data structures you use to store information in a script have no effect on the way that data is printed. You could theoretically use arrays for this but it would just make your script harder to write, more unwieldy, longer and uglier and give no benefit whatsoever. – terdon Mar 3 '15 at 0:36
4

This really has nothing to do with whether the data is stored within awk using an array or not.

As mentioned by @muru, you can re-format your data into columns using the column command

awk 'NR > 1 {print $13,$14,$15}' | column -t

Alternatively, just set awk's own output field separator to TAB instead of space directly

awk 'NR > 1 {OFS="\t"; print $13,$14,$15}'

For more complicated formatting requirements, you could replace the print statement by a printf, which would allow you to specify field-widths explicitly, e.g.

awk 'NR > 1 {printf "%4s%8s%8s\n",$13,$14,$15}'

For your iostat requirement, the first thing to note is that you can limit iostat's output to only the CPU Utilization Report (omitting the Device Utilization Report) by adding the -c command line switch, e.g.

 iostat -c 1 10

Beyond that, if you want to skip the repeated blank and header lines, you could do something like

iostat -c 1 5 | awk '/^$/ || /^avg-cpu:/ {next}; {print}'
Linux 3.13.0-45-generic (T61p)  15-03-02    _x86_64_    (2 CPU)
          11.11    0.04    2.67    0.14    0.00   86.04
           2.53    0.00    1.01    0.00    0.00   96.46
           4.46    0.00    1.49    0.00    0.00   94.06
           2.99    0.00    1.00    0.50    0.00   95.52
           3.02    0.00    0.50    0.00    0.00   96.48

or you could do something fancier to print only the first % header:

$ iostat -c 1 5 | awk '/^$/ || (/^avg-cpu:/ && a) {next}; NR>1 {a=1;print}'
avg-cpu:  %user   %nice %system %iowait  %steal   %idle
          11.11    0.04    2.67    0.14    0.00   86.04
           2.02    0.00    0.51    0.00    0.00   97.47
           4.04    0.00    0.51    0.00    0.00   95.45
           2.50    0.00    1.00    0.00    0.00   96.50
           3.02    0.00    1.01    0.00    0.00   95.98
  • I'll try those now, how can I convert it to arrays anyway for my own records? – paul Mar 1 '15 at 21:49
  • when I run it with vmstat and add cat.filename after, it outputs the filtered data ok. but when I run it with iostat or sar and do the same cat.filename, it outputs nothing. Is there an iostat-type command and a cpu-type command that will print ok with this filter? – paul Mar 1 '15 at 22:38
  • How many fields are in your iostat output, and which ones are you trying to extract? Did you change the field indices (the $13,$14 and so on) appropriately? – steeldriver Mar 1 '15 at 23:28
  • there are about six, I have adjusted it, although iostat has about two lines per iteration and so when I write it to text, it comes out messy even with the formatting. rather than print $1, $2 etc, is there a way to print the column name e.g. print nice % and skip all of the extra information? – paul Mar 2 '15 at 19:20
  • Well there's no single 'magic formula' that will turn arbitrary command output into a chosen format - you will need to familiarize yourself with the various tools (sed, awk, perl, column, ...) and their parsing/pattern matching capabilities. If you want help with a specific requirement then you will need to post an example of input and desired output i.e. exactly what you want to turn into what. – steeldriver Mar 2 '15 at 19:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.