Comparing date values stored in a file

Hi all,
I have written a script which stores data along with requisite dates in a file.
I need to compare the stored date values in the file to obtain the row with the highest date value i.e. the recent most entered record in the file.
Please help cause i dont know how we can compare dates in such case.

Can you provide a sample of the file? What is the format of each line and the date stamp.

Does the data already exist or are you also trying to define the data format?

After retrieving you values from the file. You can compare the date values with something like this:

 compvalue=`date -s "2010/05/05" +%s` 

That will convert it to seconds. The highest value wins! :slight_smile:

date "+%M%S" - only print minutes and seconds seconds. {the highest the value ) if only seconds doesnt solves ur purpose..

Check posting #2 :b:

sumi_mn, I think till you provide the sample data, a concrete solution cannot be given.

Still why don't you use awk and take the $n value to compare between the two files .

that is also anther way of using awk and comparing the date values.

all of us here are more intrested in knowing what he actually wants as in if the person can provide a sample of input and output (Desired)

Have you checked the FAQ. See http://www.unix.com/answers-frequently-asked-questions/13785-yesterdays-date-date-arithmetic.html
for an extended discussion which likely has an answer for you.

Also - post an example of your input, and your desired output, and show what you have tried so far.

Cheers

read the file line by line in a loop.

each line will have the "date value". extract them using awk or cut.

compare them to find the greatest value and store the corresponding file name in a variable.

print the value of that variable after the loop terminates.

algo:-

set a=0

read line in loop
{
set val= the reqd. date valule extracted from each line
set file= the corresponding filename extracted from each line

if (val>a) then (set a=val and latest= file name extracted)
}

print latest

convert this algo into a script.

"you may also use the find command with a newer option. this will help you avoid the script..thanks."

date +'%Y%m%d%H%M%S'

Storing date in this format can then easily be compared

noboby@nowhere# FIRSTTIMESTAMP=`date +'%Y%m%d%H%M%S'`
noboby@nowhere# SECONTIMESTAMP=`date +'%Y%m%d%H%M%S'`

noboby@nowhere# if [ $FIRSTTIMESTAMP -gt $SECONTIMESTAMP ]
noboby@nowhere> then
noboby@nowhere> echo  "$FIRSTTIMESTAMP is more recent than $SECONTIMESTAMP"
noboby@nowhere> else
noboby@nowhere> echo "$SECONTIMESTAMP is more recent than $FIRSTTIMESTAMP"
noboby@nowhere> fi
20101008203835 is more recent than 20101008203820
noboby@nowhere#