Weird date difference problem


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Weird date difference problem
# 1  
Old 03-11-2008
Java Weird date difference problem

I am trying to find the difference in days between 2 dates.
I have to extract the 1st date from a filename, which i did using the awk command.
I have to compare this date to today's date and if the difference is greater than 30 days, do something, else do something else.

This is what i wrote


Code:
INVDATE=`echo "SHIP_606400_2008233202_20080206_070308083544.xml"|awk -F_ '{print $4}'`
echo $INVDATE
TODAY=`date +"%Y%m%d"`
echo $TODAY
(( diff=$TODAY-$INVDATE))
echo $diff
if [ $diff -gt 30 ]; then
     #do a
else
  #do b
fi

The result that I got was

Code:
20080206
20080311
105

The difference between the dates is not 105 days, but 34 days.

What am i doing wrong - I am pretty new to Shell programming and am learning through this site.

Thanks in advance.
# 2  
Old 03-11-2008
It's because it's subtracting one number from the other. The subtraction you're doing has no concept of dates.

There are many ways to go about this. What exactly are you trying to do?

A couple of possibilities:

A find command using the -mtime and -exec options (you can search for all files over 30 days old and do something to them).

A short script which returns the difference in days between a file's last-modified time and the current system time, to be embedded in your script.

As usual, providing as much information as possible will get you the best results. Also, try searching this site -- your question has probably been answered many times. It depends on exactly what you're trying to do.

ShawnMilo
# 3  
Old 03-11-2008
Shawn,
Thanks for the quick response. This is what I am trying to do - these XML files are shipment files and these shipments need to be loaded into a database, dependent on some conditions.
This XML can be created any day(hence the last modified date is not an option), but it has to be loaded into the database only if the shipment is for within the last 30 days, otherwise it has to be archived to a different folder, without loading into the datatbase.
Is there any way to convert these numbers to date in UNIX (a very warped up way would be to connect to the database and do the calculation there - very unsophisticated)
Thanks again for your help.
# 4  
Old 03-11-2008
Okay, I think I get it now. You have a directory with any number of XML files. You need to read them to find out the date (stored as text) from a certain position in a record in the XML file. Depending on whether that date falls within a certain range, you either want to process the file into your database, or move the whole XML file to another directory, where it will wait to be checked later.

Is that correct?

Can there be multiple orders per XML file? If so, what if there are different dates for the different orders?

Please post an example of the XML file (if it's small enough), or at least the line(s) with the date you are trying to check. If there are multiple lines in the XML file which do not have dates, explain how the program should figure out if a particular line should be checked or ignored.

ShawnMilo
# 5  
Old 03-12-2008
This is a bit similar to an older post: https://www.unix.com/shell-programmin...two-dates.html

I would suggest convert both the dates to "epoch" seconds, then do a awk operation to convert the epoch diff secs to num days.

something like:

Code:
$ cat days.sh
D1=`echo "SHIP_606400_2008233202_20080206_070308083544.xml"|awk -F_ '{print $4}'`
E1=`date +%s -d"$D1"`
E2=`date +%s` #present time
((diff_sec=E2-E1))
echo - | awk -v SECS=$diff_sec '{printf "Number of days : %d",SECS/(60*60*24)}'

Code:
$ sh days.sh
Number of days : 35

//Jadu
# 6  
Old 03-12-2008
Jadu -
That requires GNU date, so you should mention that not all UNIX boxes have it as what you show "exceeds" POSIX standards.
# 7  
Old 03-12-2008
HI Shawn,
NO- the date is in the filename itself - SHIP_606400_2008233202_20080206_070308083544.xml - in this case "20080206".
Also, there is only 1 shipment per XML file for the date mentioned in the file name.

Thanks for your help. Is there any way to convert a string to a Julian date?


Quote:
Originally Posted by ShawnMilo
Okay, I think I get it now. You have a directory with any number of XML files. You need to read them to find out the date (stored as text) from a certain position in a record in the XML file. Depending on whether that date falls within a certain range, you either want to process the file into your database, or move the whole XML file to another directory, where it will wait to be checked later.

Is that correct?

Can there be multiple orders per XML file? If so, what if there are different dates for the different orders?

Please post an example of the XML file (if it's small enough), or at least the line(s) with the date you are trying to check. If there are multiple lines in the XML file which do not have dates, explain how the program should figure out if a particular line should be checked or ignored.

ShawnMilo
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Weird awk problem

Hi, I have a simple awk script: BEGIN{} { $a=$2-$1; print $a } END{if(NR==0){ print "0" } } to which I provide the following input 2.9 14 22.2 27 (4 Replies)
Discussion started by: jamie_123
4 Replies

2. Shell Programming and Scripting

Weird difference in script execution

Hi, So I have a very simple script which loops over 5 times and prints the iterator value. #!/bin/sh START=1 END=5 for i in $(eval echo "{$START..$END}") do echo "$i" done If I save this script in a .sh file and run it in the terminal, the output I get is {1..5} (4 Replies)
Discussion started by: jamie_123
4 Replies

3. Shell Programming and Scripting

A very weird problem about getting a random string

Hi, guys Here is my script and it's an imaginary script to recast a string randomly. #!/bin/bash # scriptname: recast_string # purpose: recast a string randomly like this "abc123" --> "21a3cb" or "a31b2c" function recast() { local original_string=$1 local... (4 Replies)
Discussion started by: franksunnn
4 Replies

4. Programming

A weird problem with POSIX function

Hi all, Sorry for the title because I didn't find a proper name for it. My question is about POSIX functions, such as timer_create(), mq_open() and pthread_create(). void test_queue() { struct mq_attr attr; attr.mq_maxmsg = 10; attr.mq_msgsize = 64; mq_unlink("/my_test_queue");... (6 Replies)
Discussion started by: bus147
6 Replies

5. Shell Programming and Scripting

awk weird problem.

awk 'BEGIN{print 1.2.3.4}' 1.20.30.4 Can anyone explain why has extra "0" in the IP address? (3 Replies)
Discussion started by: newoz
3 Replies

6. Infrastructure Monitoring

Weird dependency problem!

Hi, I want to install net-snmp-devel package but i have following dependecy problem. It's very odd, i don't get it. One of packages is depended on the other one, the other one is depended on the previous one as well. :S :S Could you help me please? Here are the steps: # ls -l total... (4 Replies)
Discussion started by: oduth
4 Replies

7. UNIX for Advanced & Expert Users

Really weird delete problem

Hi, I've Ubuntu 8.04, and it has some files that I just cannot delete. I've tried everything, inode, fsck etc. Here is what the ls -li outputs root@ubuntu:/home/luser/.local/share/Trash/files/junk# ls -l ls: cannot access TRUNK_: No such file or directory ls: cannot access 2006_output.mv:... (11 Replies)
Discussion started by: nitin
11 Replies

8. Solaris

Weird crontab problem

Greetings To All! I am running Solaris 10 in a sparc environment. Here is the deal: In /var/spool/cron/crontabs, there is a cron user named "sys". If I do a crontab -l sys, it returns: # 0 * * * 0-6 /usr/lib/sa/sa1 # 20,40 8-17 * * 1-5 /usr/lib/sa/sa1 # 5 18 * * 1-5 /usr/lib/sa/sa2... (8 Replies)
Discussion started by: RobSand
8 Replies

9. Shell Programming and Scripting

Weird problem with output from "date '+3600*%H+60*%M+%S' "

Hi, I came across a script a few months ago that allowed you to use the following script to include the current time into your prompt (useful from auditting purposes): # Set Prompt typeset -RZ2 _x1 _x2 _x3 let SECONDS=$(date '+3600*%H+60*%M+%S')... (5 Replies)
Discussion started by: m223464
5 Replies

10. UNIX for Dummies Questions & Answers

Weird Problem???

I have a problem I don't understand... I am trying to declare a variable, and then output the results of that variable, couldn't be simpler #!/bin/ksh VAR='Oranges' if then echo "Found Lemons" elif then echo "Found Oranges" fi The output shouold clearly be "Found Oranges", but... (2 Replies)
Discussion started by: danhodges99
2 Replies
Login or Register to Ask a Question