Search this site


Metadata

Articles

Projects

Presentations

statistic deltas using awk

Short shell script I call 'delta' - It is useful for groking 'vmstat -s' output (and possibly other commands) to view time-based deltas on each counter.
#!/bin/sh
while :; do
   $* | sed -e 's/^ *//';
   sleep 1;
done | awk '
{
   line = substr($0, length($1)+1);

   if (foo[line]) {
      printf("%10d %s\n", $1 - foo[line], line);
   }
   foo[line] = $1;
   fflush();
}'
Example usage:
delta vmstat -s | grep -E 'system calls|fork'

       792  system calls
         3   fork() calls
         0  vfork() calls
       120  pages affected by  fork()
         0  pages affected by vfork()
       680  system calls
         3   fork() calls
         0  vfork() calls
       120  pages affected by  fork()
         0  pages affected by vfork()
      1150  system calls
         3   fork() calls
         0  vfork() calls
       120  pages affected by  fork()
         0  pages affected by vfork()


2 responses to 'statistic deltas using awk'

Showing last 2 comments... (Click here to view all comments)

nathan wrote at Tue Jul 28 22:27:56 2009...
Hey there -

Thought I'd let you know your delta script has a typo in it - You have a double closing brace that should be a single.

  line = substr($0, length($1)+1));

should be

  line = substr($0, length($1)+1);

Also - Under Solaris, you need to use nawk

Oh - and the pipe should be on the same line as the done. :)

Awesome script though. Saw it, stole it, fixed it, loved it.

Jordan Sissel wrote at Tue Jul 28 22:37:53 2009...
Good catch. Fixed!


Leave a reply

You need javascript enabled to use this form. Anti-spam efforts ongoing. Also, if the comment doesn't show up, it's because the form expired. Go back and copy your comment, reload the form, and resubmit. Apologies if this is a hassle, I'm just playing with antispam methods right now. If this insists on not working, please email me about it.

Name (required)
E-mail (optional, if you want me to be able to email you back)
URL (also optional)
Comment: