Click to See Complete Forum and Search --> : replacing dates using cal??


Linux_cat
01-10-2005, 06:41 AM
Does anyone know how to do the following:


If I have three lines of text in a record:
919003|2002-12-04-00:00|T01003|HTUHKP|W2002| 0| 0
919003|2002-12-11-00:00|T01003|HTUHKP|W2002| 0| 0
919003|2002-12-18-00:00|T01003|HTUHKP|W2002| 0| 0

how would I change the date so that it is the monday of the week, so 2002-12-04-00:00 would become 2002-12-2-00:00??, I have to do this with multiple records and different months and dats so I can group data together....any ideas??...

I am guessing that I can usethe cal command??, but how??..

PLEASE HELP!!!.

Thanks.

Linux_cat
01-11-2005, 06:06 AM
Can someone please help???, point me on the right direction???, start me off???, PLEASE??.

bwkaz
01-11-2005, 09:09 PM
Please don't double post.

As for your problem, I think it would be MUCH easier to do this in something other than shell (because other scripting languages have types for dates which can parse them, and do arithmetic on them). Perhaps Python or Perl?

Linux_cat
01-12-2005, 04:51 AM
cheers mate, I will not double post again. AM looking into Perl now.

chrism01
01-12-2005, 09:19 PM
In Perl
use Date::Calc qw(Day_of_Week);
See Soln 3.6 in Perl cookbook
:)

Linux_cat
01-13-2005, 05:37 AM
Thanks for your help, but i have dont it in shell, using a web resource, i will post my script when it is finished.

Linux_cat
01-13-2005, 10:04 AM
here it is, I havent changed the comments that where there before from a vut and paste but have modified the script to do what I want it. Which is to search a line of text for a day, then addd to the start day to give me a new departure date, I hope that its useful to someone one day!!!!.

If anyone wants to write a function to cut down the code be my guest as I have no clue on how to do it, anyone know of a good resource that describes how to write functions??


#!/bin/ksh


# This function returns the Astro-Julian Date. The Julian
# date (JD) is a continuous count of days from 1 January 4713 BC.
# The following algorithm is good from years 1801 to 2099.
# See URL:
# http://aa.usno.navy.mil/faq/docs/JD_Formula.html
# for more information
# arguments: $1 = day, $2 = month, $3 = year in format YYYY
get_astro_JD()
{
typeset -i JDD

JDD=$(($1-32075+1461*($3+4800+($2-14)/12)/4+367*($2-2-($2-14)/12*12)/12-3*(($3+4900+($2-14)/12)/100)/4))
echo $JDD
}

# This function computes the gregorian date from the julian date - $1.
# Returns a date string in the form: MONTH DAY YEAR
# See URL:
# http://aa.usno.navy.mil/faq/docs/JD_Formula.html
# for more information
get_greg_from_JD()
{
typeset -i L
typeset -i N
typeset -i I
typeset -i J
typeset -i DAY
typeset -i MON
typeset -i YR

L=$(($1+68569)) # $1 is the julian date
N=$((4*L/146097))
L=$((L-(146097*N+3)/4))
I=$((4000*(L+1)/1461001))
L=$((L-1461*I/4+31))
J=$((80*L/2447))
DAY=$((L-2447*J/80))
L=$((J/11))
MON=$((J+2-12*L))
YR=$((100*(N-49)+I+L))

echo $MON $DAY $YR
}


YMOUT015=${1:?"requires an argument" }


while read line_text
do

if echo $line_text|grep '^.[0-9][0-9]MON.200[1-6]' > /dev/null
then
TY=$(echo $line_text|nawk -F'[|-]' '{print $5}') # Year for today - MUST be YYYY
TM=$(echo $line_text|nawk -F'[|-]' '{print $6}') # Month for today
TD=$(echo $line_text|nawk -F'[|-]' '{print $7}') # Day for today

JD=$(get_astro_JD TD TM TY) # today's Astro-Julian date
# yesterday's date
yesterdays_date_str=$(get_greg_from_JD $((JD+0)) )
# parse yesterdays date string
set - $(echo $yesterdays_date_str)
new_date=$(printf "%d-" $3
printf "%02d-" $1
printf "%02d-00:00\n" $2)
echo $line_text|nawk 'BEGIN{FS="|"; OFS="|"}{$5 ="'$new_date'"; print}' >> YMOUT15DEP

elif echo $line_text|grep '^.[0-9][0-9]TUE.200[1-6]'> /dev/null
then

TY=$(echo $line_text|nawk -F'[|-]' '{print $5}') # Year for today - MUST be YYYY
TM=$(echo $line_text|nawk -F'[|-]' '{print $6}') # Month for today
TD=$(echo $line_text|nawk -F'[|-]' '{print $7}') # Day for today

JD=$(get_astro_JD TD TM TY) # today's Astro-Julian date
# yesterday's date
yesterdays_date_str=$(get_greg_from_JD $((JD+1)) )
# parse yesterdays date string
set - $(echo $yesterdays_date_str)
new_date=$(printf "%d-" $3
printf "%02d-" $1
printf "%02d-00:00\n" $2)
echo $line_text|nawk 'BEGIN{FS="|"; OFS="|"}{$5 ="'$new_date'"; print}' >> YMOUT15DEP

elif echo $line_text|grep '^.[0-9][0-9]WED.200[1-6]' > /dev/null
then

TY=$(echo $line_text|nawk -F'[|-]' '{print $5}') # Year for today - MUST be YYYY
TM=$(echo $line_text|nawk -F'[|-]' '{print $6}') # Month for today
TD=$(echo $line_text|nawk -F'[|-]' '{print $7}') # Day for today

JD=$(get_astro_JD TD TM TY) # today's Astro-Julian date
# yesterday's date
yesterdays_date_str=$(get_greg_from_JD $((JD+2)) )
# parse yesterdays date string
set - $(echo $yesterdays_date_str)
new_date=$(printf "%d-" $3
printf "%02d-" $1
printf "%02d-00:00\n" $2)
echo $line_text|nawk 'BEGIN{FS="|"; OFS="|"}{$5 ="'$new_date'"; print}' >> YMOUT15DEP

elif echo $line_text|grep '^.[0-9][0-9]THU.200[1-6]'> /dev/null
then

TY=$(echo $line_text|nawk -F'[|-]' '{print $5}') # Year for today - MUST be YYYY
TM=$(echo $line_text|nawk -F'[|-]' '{print $6}') # Month for today
TD=$(echo $line_text|nawk -F'[|-]' '{print $7}') # Day for today

JD=$(get_astro_JD TD TM TY) # today's Astro-Julian date
# yesterday's date
yesterdays_date_str=$(get_greg_from_JD $((JD+3)) )
# parse yesterdays date string
set - $(echo $yesterdays_date_str)
new_date=$(printf "%d-" $3
printf "%02d-" $1
printf "%02d-00:00\n" $2)
echo $line_text|nawk 'BEGIN{FS="|"; OFS="|"}{$5 ="'$new_date'"; print}' >> YMOUT15DEP

elif echo $line_text|grep '^.[0-9][0-9]FRI.200[1-6]' > /dev/null
then

TY=$(echo $line_text|nawk -F'[|-]' '{print $5}') # Year for today - MUST be YYYY
TM=$(echo $line_text|nawk -F'[|-]' '{print $6}') # Month for today
TD=$(echo $line_text|nawk -F'[|-]' '{print $7}') # Day for today

JD=$(get_astro_JD TD TM TY) # today's Astro-Julian date
# yesterday's date
yesterdays_date_str=$(get_greg_from_JD $((JD+4)) )
# parse yesterdays date string
set - $(echo $yesterdays_date_str)
new_date=$(printf "%d-" $3
printf "%02d-" $1
printf "%02d-00:00\n" $2)
echo $line_text|nawk 'BEGIN{FS="|"; OFS="|"}{$5 ="'$new_date'"; print}' >> YMOUT15DEP

elif echo $line_text|grep '^.[0-9][0-9]SAT.200[1-6]' > /dev/null
then

TY=$(echo $line_text|nawk -F'[|-]' '{print $5}') # Year for today - MUST be YYYY
TM=$(echo $line_text|nawk -F'[|-]' '{print $6}') # Month for today
TD=$(echo $line_text|nawk -F'[|-]' '{print $7}') # Day for today

JD=$(get_astro_JD TD TM TY) # today's Astro-Julian date
# yesterday's date
yesterdays_date_str=$(get_greg_from_JD $((JD+5)) )
# parse yesterdays date string
set - $(echo $yesterdays_date_str)
new_date=$(printf "%d-" $3
printf "%02d-" $1
printf "%02d-00:00\n" $2)
echo $line_text|nawk 'BEGIN{FS="|"; OFS="|"}{$5 ="'$new_date'"; print}' >> YMOUT15DEP

elif echo $line_text|grep '^.[0-9][0-9]SUN.200[1-6]' > /dev/null
then

TY=$(echo $line_text|nawk -F'[|-]' '{print $5}') # Year for today - MUST be YYYY
TM=$(echo $line_text|nawk -F'[|-]' '{print $6}') # Month for today
TD=$(echo $line_text|nawk -F'[|-]' '{print $7}') # Day for today

JD=$(get_astro_JD TD TM TY) # today's Astro-Julian date
# yesterday's date
yesterdays_date_str=$(get_greg_from_JD $((JD+6)) )
# parse yesterdays date string
set - $(echo $yesterdays_date_str)
new_date=$(printf "%d-" $3
printf "%02d-" $1
printf "%02d-00:00\n" $2)
echo $line_text|nawk 'BEGIN{FS="|"; OFS="|"}{$5 ="'$new_date'"; print}' >> YMOUT15DEP

fi

done < $YMOUT015

# end script


ENJOY, and feel free to comment!!!.

Thanks everyone for their help with this, its much appreciated trust me!!!!.