Regex 使用awk将数据强制转换为gnuplot格式的正则表达式

Regex 使用awk将数据强制转换为gnuplot格式的正则表达式,regex,awk,gnuplot,Regex,Awk,Gnuplot,我正努力从中获得: 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 使用正则表达式和 1我可以使用/temp=/识别temp=但是如何在=符号后打印出4个字符?为什么不试试sed 如果这是您的实际输入,那么您可以尝试下面的awk命令 $ awk -F"[ =']" '

我正努力从中获得:

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
使用正则表达式和

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

为什么不试试sed

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

$ awk -F"[ =']" '{print $4,$9}' file
09:18:01 51.9
你为什么不试试塞德

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

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

将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

将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

虽然这是个老问题。。。为什么使用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

虽然这是个老问题。。。为什么使用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

因为我不知道。非常感谢您,这在命令行中起作用。sed-r的s/*[0-9]{2}:[0-9]{2}:[0-9]{2}.*.temp=[0-9.]+.*/\1\2/g'/var/log/rebootlogfile.log在命令行中起作用,但当我像plot一样在plot中放置sed命令时,我正试图去除一些东西,并满怀希望地让它们工作起来,所以我做了这个:阴谋,因为我不知道。非常感谢,这在命令行中起作用。sed-r的s/*[0-9]{2}:[0-9]{2}:[0-9]{2}.*.temp=[0-9.]+.*/\1\2/g'/var/log/rebootlogfile.log在命令行中起作用,但是当我像plot一样在plot中放置sed命令时,我正试图去除一些东西,并满怀希望地得到一些工作,所以我这样做了:plot