Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
261 views
in Technique[技术] by (71.8m points)

linux - Using awk to find data matching date range in text file

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.

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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 )

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...