Good afternoon to you all
I need your help
I need a shell script that reads a date and then returns the immediate next date.
For example:
I have a file dates.txt containing these dates:
"2010-03-21 22:30:00"
"2010-03-18 21:10:00"
"2010-03-03 14:42:00"
"2010-04-28 09:30:10"
What I want to do, is to define a date="2010-03-17 20:00:00" and parse throught the dates.txt and find de imediate next date (in this case it would be "2010-03-18 21:10:00").
Can you please help me
Thkx in advance
check the FAQ article:
http://www.unix.com/answers-frequently-asked-questions/13785-yesterdays-date-date-arithmetic.html
Think you should find some answers in there.
HTH
---------- Post updated at 03:27 PM ---------- Previous update was at 03:04 PM ----------
Having said that....I got interested
the following (bash)code snippet should be close to what you're after:
d="2010-03-17 20:00:00"
dn=$(echo $d | tr -d ":- ")
t=$dn
while read s
do
dd=$(($(tr -d ":- \""<<<$s)-$dn))
[[ $dd -gt 0 ]] && [[ $dd -lt $t ]] && t=$dd && ss=$s
done < infile
echo $ss
"2010-03-18 21:10:00"
HTH:D
kshji
May 14, 2010, 11:32am
3
Change to epoc and use it.
file
2010-03-21 22:30:00
2010-03-18 21:10:00
2010-03-03 14:42:00
2010-04-28 09:30:10
Using gnu date:
#!/bin/ksh93 or bash or ...
cat file | while read line
do
day=$(date -u --date="$line" +"%s")
((day-=86400))
yesterday=$( date -u --date="1970-01-01 $day seconds" '+%Y-%m-%d H:%M:%S' )
echo "$line => $yesterday"
done
Or ksh93 printf
#!/bin/ksh93
cat file | while read line
do
day=$(printf "%(%#)T" "$line")
((day-=86400))
yesterday=$( printf "%(%Y-%m-%d %H:%M:%S)T \n" "#$day" )
echo "$line => $yesterday"
done