Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...