使用awk在文本文件中查找与日期范围匹配的数据

问题描述:

我有一个文本文件,其中包含来自五个传感器的日期戳和温度值,并且每十分钟用新的一行数据更新该文件.

I have a text file with date stamp and temperature values from five sensors and every ten minutes the file is updated with a new row of data.

这是数据文件的示例-第1列和第2列是日期和时间,第3列到第7列是温度值:

Here is a sample of the data file - cols 1 and 2 are date and time, cols 3 to 7 are temperature values:

31-12 04:40 19.6 20.5 18.3 21.3 12.5
31-12 04:50 19.6 20.4 18.3 21.3 12.7
31-12 05:00 19.5 20.4 18.2 21.2 12.6
31-12 05:10 19.5 20.4 18.2 21.2 12.5
31-12 05:20 19.5 20.4 18.5 21.2 12.1

如何使用awk从数据文件中提取与过去24小时,过去7天,过去28天和过去365天有关的记录?

How can I use awk to extract from the data file those records that pertain to the last 24 hours, last 7 days, last 28 days and last 365 days?

坏消息:标准 awk 不具有日期处理功能,日期处理对于shell脚本来说是成败之举.GNU和BSD版本的 date 命令都可以使用date命令来检查日期,但是两者都使用完全不同的语法.

Bad news: Standard awk doesn't have date handling capabilities, and date handling is a hit or miss affair for shell scripts. Both GNU and BSD versions of the date command can use the date command to check a date, but both use completely different syntax for doing so.

如果您将 gawk 或Linux与 awk 结合使用,则可以尝试 mktime 函数:

If you're using gawk or Linux with awk, you can try the mktime function:

date="20141225011522"   # December 25, 2014 at 1:15:22
date_in_seconds = mktime( date )

您需要对日期进行一些重新格式化,但是一旦完成,您将获得自 epoch 以来的秒数(通常是1970年1月1日)

You'll need to do a bit of reformatting with your dates, but once done, you'll get back the date in the number of seconds since the epoch which is usually January 1, 1970.

顺便说一句,您需要在代码本身中包括示例以及所遇到的问题,否则您的问题将被解决.

By the way, you need to include examples of what you tried, and the problems you ran into in the code itself, or else your question will be closed.