LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 09-05-2013, 09:40 PM   #1
NetEng1
Member
 
Registered: Aug 2011
Distribution: ubuntu
Posts: 37

Rep: Reputation: Disabled
Bash script time comparison with one being a text string


I am trying to compare a time within a file to the system time to see long since the date in the file has passed. I am trying to do this in bash. The date in the file is in the following format:

"September 05, 2013 9:18 PM"

I tried something like:

grep "last updated" index.htm

mHour=`grep "last updated" index.htm|awk '{print $8}'`

cmHourSeconds=`date --date $mHour +%s`
cmHour=`date --date $mHour`

myHourSeconds=`date +%s`
myHour=`date`

echo cmHourSeconds is $cmHourSeconds
echo cmHour is $cmHour
echo myHourSeconds is $myHourSeconds
echo myHour is $myHour

DIFF=$(($cmHourSeconds-$myHourSeconds))
echo the difference is $DIFF

hourSeconds=1440
let diffHour=$DIFF/$hourSeconds
echo the diff is $diffHour


mTime=`grep "last updated" index.htm|awk '{print $8}'`

if [[ $mTime == "PM" ]]; then

cmtHourSeconds=$(($cmHourSeconds+17280))
fi

echo $cmtHourSeconds


But it seems to be 18000 seconds off. Is there another way to accomplish this?

Thanks.
 
Old 09-05-2013, 11:18 PM   #2
NetEng1
Member
 
Registered: Aug 2011
Distribution: ubuntu
Posts: 37

Original Poster
Rep: Reputation: Disabled
Sorry, I got it to work. Here is the edited file:

mHour=`grep "last updated" index.htm|awk '{print $8}'`

cmHourSeconds=`date --date $mHour +%s`

myHourSeconds=`date +%s`

mTime=`grep "last updated" index.htm|awk '{print $9}'`

cmtHourSeconds=$(($cmHourSeconds+43200))

if [[ $mTime == "PM" ]]; then

let DIFF=$(($cmtHourSeconds-$myHourSeconds))
else

let DIFF=$(($cmHourSeconds-$myHourSeconds))
fi


if [[ $DIFF -ge 3660 || $DIFF -le -3660 ]]; then

echo MIT\'s update time is off by $DIFF
exit 3
else

echo MIT\'s update time is ONLY off by $DIFF
exit 0
fi
 
Old 09-05-2013, 11:19 PM   #3
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
you don't need grep and awk

Code:
awk '/last updated/{print $8}'  index.htm
does the same

not convinced field 8 is "September 05, 2013 9:18 PM"

to get difference in seconds
Code:
echo $(( $(date +%s) - $(date +%s --date="September 05, 2013 9:18 PM") ))
lets pretend your awk gets "September 05, 2013 9:18 PM"

Code:
SecondsSince=$(( $(date +%s) - $(date +%s --date="$(awk '/last updated/{print $8}'  index.htm)") ))

already done too much..
Code:
#!/bin/bash
SecondsSince=$(( $( date +%s ) - $( date +%s --date="September 05, 2013 9:18 PM" ) ))
HoursSince=$(( $SecondsSince / 3600 ))
RemainderSeconds=$(( $SecondsSince - $(( $HoursSince * 3600 )) ))
MinSince=$(( $RemainderSeconds / 60 ))
RemainderSeconds=$(( $RemainderSeconds - $(( $MinSince * 60 )) ))

printf "$HoursSince:$MinSince:$RemainderSeconds"
I will let you do the formatting and add days, weeks, months etc,

Last edited by Firerat; 09-05-2013 at 11:22 PM.
 
1 members found this post helpful.
Old 09-05-2013, 11:22 PM   #4
NetEng1
Member
 
Registered: Aug 2011
Distribution: ubuntu
Posts: 37

Original Poster
Rep: Reputation: Disabled
Thank you that seems a cleaner approach than what I got working.
 
Old 09-05-2013, 11:26 PM   #5
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
the trick is to get the "September 05, 2013 9:18 PM" string into it

can't see your input, so blind there
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash script- text file manip- delete everything before/after string justin99 Programming 9 11-20-2014 04:16 AM
Bash script to remove string from all text files, recursively iacchi Programming 9 04-27-2012 06:22 AM
Date comparison with 'string date having slashes and time zone' in Bash only TariqYousaf Programming 2 10-08-2009 08:37 AM
How to remove string in the text file ? Bash script dlugasx Linux - Server 9 06-05-2009 12:40 PM
bash-script: output text between two ocurrences of a specific string isl01jbe Programming 1 06-17-2004 03:36 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 10:33 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration