Find the answer to your Linux question:
Results 1 to 2 of 2
I need help trying to figure out how to use grep with an expression that is constantly changing... basically this is what I am trying to do: Code: watch "mpstat ...
  1. #1
    Just Joined!
    Join Date
    Mar 2011
    Posts
    1

    Using grep with changing expressions

    I need help trying to figure out how to use grep with an expression that is constantly changing... basically this is what I am trying to do:

    Code:
    watch "mpstat -P ALL | grep -e '`date +%r`'"
    which, should show me my CPU usage updated every couple seconds. The grep is being used to omit lines that aren't needed (all the useful lines start with the current time.) Problem is, it seems to execute the 'date +%r' command once.

    I would like grep to execute the date command every time the watch command updates.


    EDIT: I've found a workaround by putting the

    Code:
    mpstat -P ALL | grep -e "`date +%r`"
    All in a shell file (that I called cpustat.sh), and then executing watch ./cpustat.sh

    Would still like to know if there is an actual solution without having to create a seperate file (and preferably in one line). I have tried using a function to do the mpstat and grep part, but apparently watch doesn't play nice with functions
    Last edited by TyPR124; 03-20-2011 at 05:07 AM.

  2. #2
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,977
    The bash shell has loops. Run your code in the loop, and sleep between loops for a couple of seconds. You can do this from the command line - a script is not needed. You might even be able to use watch with the loop and eliminate the sleep command.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...