【问题标题】:Regular expression using awk for forcing data into gnuplot format使用 awk 将数据强制转换为 gnuplot 格式的正则表达式
【发布时间】:2014-09-24 09:49:19
【问题描述】:

我正在努力从中获得帮助:

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

到此输入 gnuplot 格式:

09:18:01 51.9

使用正则表达式和awk

1) 我可以使用 /temp=/ 识别“temp=”,但如何在 = 符号后打印出 4 个字符?

【问题讨论】:

    标签: regex awk gnuplot


    【解决方案1】:

    你为什么不试试sed

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

    如果这是您的实际输入,那么您可以尝试以下 awk 命令,

    $ awk -F"[ =']" '{print $4,$9}' file
    09:18:01 51.9
    

    【讨论】:

    • 因为我不知道。非常感谢,这可以从命令行运行。
    • sed -r 's/.*([0-9]{2}:[0-9]{2}:[0-9]{2}).*temp =([0-9.]+).*/\1 \2/g' /var/log/rebootlogfile.log 从命令行工作,但是当我执行 plot " 使用 gnuplot 它不起作用。有什么想法吗?
    • 将 sed 命令放入 () 中,如 plot "<(sed -n 'regex' file)" 参见 stackoverflow.com/a/12223810/3297613
    • 我正在尝试剥离一些东西并希望让一些东西正常工作,所以我这样做了:plot " 但是我在第一行得到错误数据 命令行给了我一列"xx.x" 应该在 gnuplot 中工作
    【解决方案2】:

    将 GNU awk 用于 gensub():

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

    与其他 awk 一起使用 match() 和 substr():

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

    使用 sed:

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

    【讨论】:

      【解决方案3】:

      虽然这是一个老问题...为什么要使用 awk 或 sed?为什么不使用 gnuplot?

      # Data.dat
      Wed Sep 24 09:18:01 BST 2014 -- temp=51.9'C -- message from script /usr/src/scripts/wifi_test_2.sh
      Wed Sep 24 10:19:02 BST 2014 -- temp=53.1'C -- message from script /usr/src/scripts/wifi_test_2.sh
      Wed Sep 24 11:20:03 BST 2014 -- temp=55.2'C -- message from script /usr/src/scripts/wifi_test_2.sh
      

      代码(也适用于 gnuplot 4.6.0)

      ### extracting data
      reset
      
      FILE = "Data.dat"
      set xdata time
      set datafile separator " "
      set timefmt "%H:%M:%S"
      
      # function for extracting number between characters
      ExtractNumber(s,c1,c2) = real(s[strstrt(s,c1)+1:strstrt(s,c2)-1])
      
      plot FILE u (strcol(4)):(ExtractNumber(strcol(8),"=","'")) w lp pt 7 not
      ### end of code
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-27
        • 2013-01-04
        • 2011-10-02
        • 2016-04-02
        • 1970-01-01
        • 2019-05-11
        相关资源
        最近更新 更多