Search code examples
regexawkgnuplot

Regular expression using awk for forcing data into gnuplot format


I'm relay struggling to get from this:

Wed Sep 24 09:18:01 BST 2014 -- temp=51.9'C -- message from script /usr/src/scripts/wifi_test_2.sh

To this for input into gnuplot format:

09:18:01 51.9

Using regular expression and awk

1) I can recognize the "temp=" using /temp=/ but how can I print out 4 characters after the = sign?


Solution

  • Using GNU awk for gensub():

    $ awk '{print $4, gensub(/.*temp=([0-9.]+).*/,"\\1","")}' file
    09:18:01 51.9
    

    With other awks use match() and substr():

    $ awk '{print $4, substr($0,match($0,/temp=[0-9.]+/)+5,RLENGTH-5)}' file
    09:18:01 51.9
    

    With sed:

    $ sed -r 's/.*(..:..:..).*temp=([0-9.]+).*/\1 \2/' file
    09:18:01 51.9