1846873 Members
2974 Online
110256 Solutions
New Discussion

Re: diff - scripts

 
SOLVED
Go to solution
cfeitosa
Frequent Advisor

diff - scripts

Hello guys!

Please I need a help.

I'm using HPUX 11

I have two files called 05_05_2007.txt and 06_05_2007.txt in the directory /tmp

-rw-rw-rw- 1 F0104582 fnusr 22 May 23 10:56 05_05_2007.txt
-rw-rw-rw- 1 F0104582 fnusr 59 May 23 10:57 06_05_2007.txt

I need to do a script that to compare theses two files.
BUT everday theses files will change his names because I create according with the currente date

Today I need to compare the file of today with the file of yesterday and tomorrow the same thing.
In other words, everyday I need to compare the actual file with the file of the day before.

For exampple:
#:/diff 05_05_2007.txt 06_05_2007.txt > a.txt

I don't know how can I get theses files in the directory.

#:/diff $A $B > a.txt

How can I do it automatically? I will put this script in the crontab.

Please could someone give me a help?

Thanks a lot!
clefeitosa
10 REPLIES 10
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: diff - scripts

First download the attached script, caljd.sh, and install it in a convenient directory such as /usr/local/bin.

The rest is then trivially simple.

----------------------------------------
#!/usr/bin/sh

export PATH=${PATH}:/usr/local/bin

TODAY="$(caljd.sh -e -S "_" $(caljd.sh)).txt"
YESTERDAY="$(caljd.sh -e -S "_" $(caljd.sh -p 1)).txt"

echo "Today File = ${TODAY}"
echo "Yesterday File = ${YESTERDAY}"
---------------------------------------

Invoke as caljd.sh -u for full usage and many examples.
If it ain't broke, I can fix that.
cfeitosa
Frequent Advisor

Re: diff - scripts

Hi Clay,

Thanks for your answer but I don't install something. This script is very good but I think that isn't necessary for me. I would like something simplier.

Anyway, thank you very much... I'm studying about it and if someone knows anything more, please...
Ivan Ferreira
Honored Contributor

Re: diff - scripts

You could play with the TZ environment variable, for example:

# export TZ="GMT"
# date +%d_%m_%Y
23_05_2007
# export TZ="GMT+24"
# date +%d_%m_%Y
22_05_2007
# unset TZ
Por que hacerlo dificil si es posible hacerlo facil? - Why do it the hard way, when you can do it the easy way?
spex
Honored Contributor

Re: diff - scripts

Hello,

ls -1 [0-3][0-9]_[01][0-9]_2[0-9][0-9][0-9].txt | sort -n -t'_' -k3 -k2 -k1 | tail -n 2 | xargs diff > a.txt

PCS
A. Clay Stephenson
Acclaimed Contributor

Re: diff - scripts

Subtracting 24 hours from TZ, if it works, it works by accident, and is very dependent upon your TZ.

Since you didn't like caljd.sh (I assume you did a chmod 755 caljd.sh after you copied it) then this shell script which calls a Perl one-liner should work:

-----------------------------------------
#!/usr/bin/sh

TODAY=$(perl -e '($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time()); printf("%02d_%02d_%04d.txt\n",$mday,$mon + 1,$year + 1900)')


YESTERDAY=$(perl -e '($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time() - (86400 * 1)); printf("%02d_%02d_%04d.txt\n",$mday,$mon + 1,$year + 1900);')

echo "Today File = ${TODAY}"
echo "Yesterday File = ${YESTERDAY}"

-----------------------------------------

Now, caljd.sh (or the equivalent caljd.pl Perl version) is a big script but who cares? You only call it --- and if you start using it for date calculations of any kind, I'm pretty sure that you won't use anything else.
If it ain't broke, I can fix that.
James R. Ferguson
Acclaimed Contributor

Re: diff - scripts

Hi:

If you simply want today and yesterday returned in the format "dd_mm_yyyy":

# TODAY=`perl -MPOSIX -le 'print strftime "%d_%m_%Y",localtime'`

# YESTERDAY=`perl -MPOSIX -le 'print strftime "%d_%m_%Y",localtime(time-86400)'`

Regards!

...JRF...
Andrew Merritt_2
Honored Contributor

Re: diff - scripts

If it's always going to be the two most recent files in the directory, then this should work:

diff `ls -t|head -2`

(Note that those are back-quotes. The ls will list with the most recent file first, and the head will give the first two.)

Andrew

cfeitosa
Frequent Advisor

Re: diff - scripts

Hello!

Thank you very much for all answers!!!
Was very useful!

I'll use Andrew's suggestion.

Best regards,
clefeitosa
cfeitosa
Frequent Advisor

Re: diff - scripts

Thanks
Dennis Handly
Acclaimed Contributor

Re: diff - scripts

>Andrew: (Note that those are back-quotes.

If you used the following you wouldn't have to say that:
$ diff $(ls -t|head -2)

>BUT everday theses files will change his names because I create according with the current date

If you have more complicated case where the files are changing, you could try setting symlinks to point to the last 2 files. Have a LAST link and a PREV link. When creating a new file, remove PREV, rename LAST to PREV and link the new name to LAST.

Note it would be better to create the files with YYYY_MM_DD.

A simple script using sed can be used to rename all of your existing files.