I need a help, please


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting I need a help, please
# 1  
Old 10-28-2006
Tools I need a help, please

I need your help please.
i have a function which has an input file called lista1 and the format is this:

lista1:

01/09/2006
01/09/2006
01/09/2006
01/09/2006
01/09/2006
01/09/2006
01/09/2006
01/09/2006
01/09/2006
01/09/2006
01/09/2006
01/09/2006
01/09/2006
01/09/2006
01/09/2006
02/08/2006
i tried to modify the records to direct to list2 but the output from this file lista2 is:
1/8/2006
1/8/2006
1/8/2006
1/8/2006
1/8/2006
1/8/2006
1/8/2006
1/8/2006
1/8/2006
1/0.00398804
1/0.00398804
1/0.00398804
1/0.00398804
1/0.00398804
1/0.00398804
1/0.00398804
1/0.00398804
1/0.00398804
1/0.00398804
1/0.00398804
1/0.00398804
1/0.00398804
1/0.00398804
1/0.00398804
1/0.00398804
1/0.00398804
1/0.00398804
1/0.00398804
Ptroduce strange records.
Why:
I just want to less one day ie if the record from lista1 is 04/08/2006, i want this record could become in lista2 03/08/2006



ajusta_fecha()
{
cut -f1 -d " " listacdrs > lista1
valida=`cut -f2 -d "/" lista1|uniq|grep -v $mes lista1|uniq`
if [ $? = 0 ]
then
awk -v mes=$mes '
BEGIN {FS="/"}
$1>1 && $2=mes {dia=$1-1;print dia"/"$2/$3}
$1<=1 && $2!=mes {prev=dia+1;mes=$2-1;print prev"/"mes"/"$3}' lista1> lista2
fi
}
# 2  
Old 10-29-2006
If you just want to less one day from your record, why don't you try Perderabo's datecalc , here is one dirty code for the requirement mentioned by you:
Code:
#! /bin/ksh

#Delete contents of file lista2
cat /dev/null > lista2

oldIFS=$IFS
IFS="/"
while read day month year; do
./datecalc -a $year $month $day - 1 | awk '{ printf "%02d/%02d/%04d\n", $3, $2, $1 }' >> lista2
done < lista1
IFS=$oldIFS

Input:
Code:
$cat lista1
01/09/2006
01/09/2006
01/09/2006
01/09/2006
01/09/2006
01/09/2006
01/09/2006
01/09/2006
01/09/2006
01/09/2006
01/09/2006
01/09/2006
01/09/2006
01/09/2006
01/09/2006
02/08/2006

Output:
Code:
$cat lista2
31/08/2006
31/08/2006
31/08/2006
31/08/2006
31/08/2006
31/08/2006
31/08/2006
31/08/2006
31/08/2006
31/08/2006
31/08/2006
31/08/2006
31/08/2006
31/08/2006
31/08/2006
01/08/2006

Above code works little slow, maybe someone else could come up with some faster code.
Regards,
Tayyab
# 3  
Old 10-29-2006
This should be a little faster...
Code:
#! /bin/ksh
typeset -Z2 month2 day2                                  # month2 and day2 will be 2 columns with leading zeros inserted as needed
while IFS=/ read day month year; do                      # loop reading variable separated by a /
        set +A a $(./datecalc -a $year $month $day - 1)  # Set the array a to the output of the datecalc command 
        day2=${a[2]}                                     # day2 will get a leading zero if needed
        month2=${a[1]}                                   # month2 will get a leading zero if needed
        echo ${day2}/${month2}/${a[0]}                   # output the answer
done < lista1 >lista2                                    # redirect input and output of the loop
exit 0

# 4  
Old 10-29-2006
Python Alternative:

Code:
import datetime,time
difference = datetime.timedelta(days=-1)  #1 day diff
data = open("file").readlines()
for date in data:
        date = date.strip()
        d = time.strptime(date,"%d/%m/%Y")[0:3]
        now = datetime.date(d[0], d[1], d[2])
        yesterday = now + difference
        print yesterday

output:
Code:
2006-08-31
2006-08-31
2006-08-31
2006-08-31
2006-08-31
2006-08-31
2006-08-31
2006-08-31
2006-08-31
2006-08-31
2006-08-31
2006-08-31
2006-08-31
2006-08-31
2006-08-31
2006-08-01

# 5  
Old 10-31-2006
Thanks a lot Perederabo, it is a great and a useful script, i just have a question if you dont mind. Could you explaine me line por line what thos shell is doing, ive triing to grasp but to be perfectly honest i dont understand. the code is this:
typeset -Z2 month2 day2
while IFS=/ read day month year; do
set +A a $(./datecalc -a $year $month $day - 1)
day2=${a[2]}
month2=${a[1]}
echo ${day2}/${month2}/${a[0]}
done < lista1 >lista2
exit 0
Thanks a lot again.
# 6  
Old 10-31-2006
I added some comments above.
Login or Register to Ask a Question

Previous Thread | Next Thread
Login or Register to Ask a Question