开发者

Parsing date and checking how recent it is in Bash

开发者 https://www.devze.com 2023-04-10 09:56 出处:网络
I\'m working on a Bash script (using Cygwin) that uses cURL to scrape a web page and check a certain date value. My cURL and grep calls result in the follow开发者_运维百科ing line:

I'm working on a Bash script (using Cygwin) that uses cURL to scrape a web page and check a certain date value. My cURL and grep calls result in the follow开发者_运维百科ing line:

<span style="float:right">Last Update: 9/30/2011 3:16:31 AM</span><p>

What I need to do with the date is check if it is within the last n days. What is the best way to approach this, and how should I parse the date?


Something like:

DATESTRING="$(sed -e 's/.*Last Update: \([^<]*\)<.*/\1/' $MYINPUT)"
UPDATE=$(date -d "$DATESTRING" +%s) 
EPOCH=$(date -d "-$n days" +%s)
test "$UPDATE" -ge "$EPOCH" && echo "It's new!"


The 'date' program should be able to parse that date format. For example:

% date -d '9/30/2011 3:16:31 AM'
Fri Sep 30 03:16:31 PDT 2011

So, you can use 'date' to convert that to something usable in bash (an integer, seconds since the epoch):

parseddate=$(something that extracts just the date from the line ...)
date -d "$parseddate" +%s

Then compare that to the result of

date -d '3 days ago' +%s


I would say that the absolute best way is to parse it with another language, for example Perl, since it will be easier to parse out the date. If you'd rather stick with Bash, check the --date option in man date.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号