4

I have the following:

2018-11-10 23:57:21 [COMMAND]: sar -u 10 5

AIX host 1 7    11/10/18

System configuration: lcpu=64  mode=Capped

23:57:21    %usr    %sys    %wio   %idle   physc
23:57:31      10       7       0      83   16.00
23:57:41       9       6       0      85   16.00
23:57:51       9       6       0      85   16.00
23:58:01       9       7       0      84   16.00
23:58:11      10       6       0      84   16.00

Average        9       6       0      84   16.00

2018-11-10 23:58:21 [COMMAND]: sar -u 10 5

AIX host 1 7    11/10/18

System configuration: lcpu=64  mode=Capped

23:58:21    %usr    %sys    %wio   %idle   physc
23:58:31      10       8       0      82   15.99
23:58:41       9       6       0      85   16.00
23:58:51       9       6       0      85   16.00
23:59:01       9       6       0      84   16.00
23:59:11      10       6       0      83   16.00

Average       10       6       0      84   16.00

I need to get the time with the average value of %idle:

2018-11-10 23:57:21|84
2018-11-10 23:58:21|84
  • actually i am getting this file from another server and it is only one file – ammar Nov 11 '18 at 13:12
  • 1
    This is using this site as a script-writing service. – Peter Mortensen Nov 12 '18 at 8:30
11

Going by your input file as-is, a simple awk command as simple as below should suffice.

awk '/sar/{ time=$1" "$2; next }/Average/{ print time"|"$5 }' file
5

A couple of choices:

  • awk

    $ awk '/COMMAND/{printf "%s %s", $1,$2}/Average/{print "|"$5}' file 
    2018-11-10 23:57:21|84
    2018-11-10 23:58:21|84
    
  • perl

    $ perl -ale '$k="$F[0] $F[1]" if /COMMAND/; print "$k|$F[4]" if /Average/' file 
    2018-11-10 23:57:21|84
    2018-11-10 23:58:21|84
    
2

This Perl one-liner should do the trick:

perl -lne'$d=$1 if /^(\d{4}-\d{2}-\d{2}\s+\d{2}:\d{2}:\d{2})/;
if(/^Average\s+\S+\s+\S+\s+\S+\s+(\S+)/) {print "$d|$1";}'

(I made it two lines, so it's more readable.) You need to feed it with input though. The file name at the end, or <file_name at the beginning or the end, or a pipe will do. The workings are these:

  1. Go to a line with date and time at the beginning and memorise it.
  2. Go to a line with Average at beginning and once there, print the memorised date and time plus the | sign and the average taken from the fifth column assuming whitespace column separation.
  3. Point 1. repeats.

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.