如何在gnuplot中读取12h(AM / PM)时间格式 [英] How to read 12h (AM/PM) timeformat in gnuplot

查看:131
本文介绍了如何在gnuplot中读取12h(AM / PM)时间格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过使用现有和自动生成的数据文件来生成一些漂亮的图形。不幸的是,我不能改变文件的格式。

这些行包含一个时间戳和一个值。例如

  2014年7月6日下午12点46分84.6 

在Gnuplot中,我可以使用给定的格式标识符来读取时间戳。但是在上午或下午的时间不可能分开。

  set timefmt%B%d,%Y at%H :%M

关于这个问题的任何建议?

awk 脚本会格式化你的文件:

  awk'{time = $ 5; if(substr(time,6,2)==PM\ 
& amp; substr(time,1,2)< 12)add = 12; else add = 0; \
$ 5 = substr(time,1,2)+ addsubstr(time,3,3); \
print $ 0}'data

这样

  2014年7月6日上午10:46 83.6 
2014年7月6日下午12:46 84.6
2014年7月6日下午10:46 85.6

看起来就像

  2014年7月6日10:46 83.6 
2014年7月6日12:46 84.6
2014年7月6日22:46 85.6

在gnuplot中,您可以在绘制文件时动态地执行此操作。你可以这样做(注意我需要转义):

 设置时间段%B%d,%Y在%H:%M
设置xdata时间
设置格式x%H:%M
plot< awk'{time = $ 5; if(substr(time,6,2)== \PM \\
& amp; substr(time,1,2)<12)add = 12; else add = 0; \
$ 5 = substr(time,1,2)+ add \\substr(time,3,3); \
print $ 0}'datau 1:6 wl


注意我假设00:15是00:15 AM如果你的输出是12:15 AM,那么你需要相应地修改这个脚本,如下所示:

  if (substr(time,6,2)==AM& amp; substr(time,1,2)== 12)add = -12 


I want to generate some nice graphs by using existing and auto generated data files. Unfortunately I cannot change the format of the files.

The lines contain a timestamp and a value. e.g.

July 06, 2014 at 12:46PM 84.6

In Gnuplot I can use the given format identifier to read the timestamp. But there is no possibility to separate between AM oder PM time.

set timefmt "%B %d, %Y at %H:%M"

Any suggestions on this problem?

解决方案

The following awk script will format your file:

awk '{time = $5; if(substr(time,6,2) == "PM" \
&& substr(time,1,2) < 12) add = 12; else add = 0; \
$5 = substr(time,1,2)+add "" substr(time,3,3); \
print $0}' data

so that

July 06, 2014 at 10:46AM 83.6
July 06, 2014 at 12:46PM 84.6
July 06, 2014 at 10:46PM 85.6

will look like

July 06, 2014 at 10:46 83.6
July 06, 2014 at 12:46 84.6
July 06, 2014 at 22:46 85.6

within gnuplot you can do this dynamically when plotting your file. You can do (note I need to escape the "):

set timefmt "%B %d, %Y at %H:%M"
set xdata time
set format x "%H:%M"
plot "< awk '{time = $5; if(substr(time,6,2) == \"PM\" \
&& substr(time,1,2) < 12) add = 12; else add = 0; \
$5 = substr(time,1,2)+add \"\" substr(time,3,3); \
print $0}' data" u 1:6 w l

Note I am assuming that 00:15 is 00:15AM. If your output is 12:15AM then you need to modify the script accordingly. Something along these lines:

if(substr(time,6,2) == "AM" && substr(time,1,2) == 12) add = -12

这篇关于如何在gnuplot中读取12h(AM / PM)时间格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆