"Next Date" Script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting "Next Date" Script
# 1  
Old 05-14-2010
"Next Date" Script

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
# 2  
Old 05-14-2010
check the FAQ article:

https://www.unix.com/answers-frequent...rithmetic.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:

Code:
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"

HTHSmilie
# 3  
Old 05-14-2010
Change to epoc and use it.

file
Code:
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:
Code:
#!/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
Code:
#!/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

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. Shell Programming and Scripting

awk "date" and "system" command

Hello experts! I need your help please I have a file.txt of which I want to extract 3rd and 4th columns with date with the form e.g.: 2016-11-25 03:14:50and pass them to "date" command, but also append the 9th column in a file as well. So I want to execute date -d '2016-11-25 03:14:50' ... (2 Replies)
Discussion started by: phaethon
2 Replies

3. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

4. UNIX for Dummies Questions & Answers

"Help with bash script" - "License Server and Patch Updates"

Hi All, I'm completely new to bash scripting and still learning my way through albeit vey slowly. I need to know where to insert my server names', my ip address numbers through out the script alas to no avail. I'm also searching on how to save .sh (bash shell) script properly.... (25 Replies)
Discussion started by: profileuser
25 Replies

5. Shell Programming and Scripting

How to increment date using "for loop" in format MMDDYY inside the shell script?

Need to increment the date from "currentdate + 90days" inside the for loop (i=1 to i=50) (5 Replies)
Discussion started by: aroragaurav.84
5 Replies

6. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

7. AIX

xx=`date +"%a %b %d"`;rsh xxx grep "^$XX" zzz ?

AIX 4.2 I am trying to do an rsh grep to search for date records inside server logs by doing this : xx=`date +"%a %b %d"` rsh xxx grep "^$XX" zzz gives : grep: 0652-033 Cannot open Jun. grep: 0652-033 Cannot open 11. But if I do : xx=`date +"%a %b %d"` grep "^$XX" zzz it works... (2 Replies)
Discussion started by: Browser_ice
2 Replies

8. UNIX for Advanced & Expert Users

add seconds to: date"|"time"|"HHMMSS

Hey all, I have a shell that invokes a AWK. In this AWK i want invoke a function that receives 3 parameters: date: 20080831 time: 235901 duration: 00023 that function receive this 3 parameters and sum to this value two more seconds: 2008083123590100025 Remember that in case that... (3 Replies)
Discussion started by: anaconga
3 Replies

9. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies
Login or Register to Ask a Question