I am modifying a Korn shell script in using the Exceed (Solaris 10 environment). My task is to read in a .txt file with dates arranged like this (01-Sep-2006). I am to read each line and take the dates, compare them to a benchmark date and depending on if it is older than the date or the date and newer, produce a result. The problem is that when the first date is read, the condition for the first date follows for the rest of the dates. This is not what I want. I want to have each date tested and produce a different result. Here is what I have:
#!/bin/ksh
#set -x
#NOW, WE OPEN THE TEXT FILE
exec 3<week_ending_dates1.txt
while read -u3 LINE
do
day=$(echo $LINE | cut -d'-' -f1)
month=$(echo $LINE | cut -d'-' -f2)
year=$(echo $LINE | cut -d'-' -f3)
case $month in
Jan*)
month=01
;;
Feb*)
month=02
;;
Mar*)
month=03
;;
Apr*)
month=04
;;
May*)
month=05
;;
Jun*)
month=06
;;
Jul*)
month=07
;;
Aug*)
month=08
;;
Sep*)
month=09
;;
Oct*)
month=10
;;
Nov*)
month=11
;;
Dec*)
month=12
;;
esac
# THE DATE IS NOW PUT BACK TOGETHER TO BE COMPARED TO THE BENCHMARK
testdate=$year$month$day
branchdate="20070814"
if [[ $testdate -lt $branchdate ]]; then
more week_ending_dates1.txt | grep Month | awk '{ print "time "$1 \n test1" > "file_"$1}'
else
more week_ending_dates1.txt | grep Month | awk '{ print "time "$1 \n test2" > "file_"$1}'
fi
done
exec 3<&-
I have modified your script for test purpose (under bash, not ksh) :
exec 3<week_ending_dates1.txt
while read -u3 LINE
do
day=$(echo $LINE | cut -d'-' -f1)
month=$(echo $LINE | cut -d'-' -f2)
year=$(echo $LINE | cut -d'-' -f3)
case $month in
Jan*) month=01 ;;
Feb*) month=02 ;;
Mar*) month=03 ;;
Apr*) month=04 ;;
May*) month=05 ;;
Jun*) month=06 ;;
Jul*) month=07 ;;
Aug*) month=08 ;;
Sep*) month=09 ;;
Oct*) month=10 ;;
Nov*) month=11 ;;
Dec*) month=12 ;;
esac
# THE DATE IS NOW PUT BACK TOGETHER TO BE COMPARED TO THE BENCHMARK
testdate=$year$month$day
branchdate="20070814"
if [[ $testdate -lt $branchdate ]]; then
echo "$LINE -> $testdate LOWER THAN $branchdate"
else
echo "$LINE -> $testdate GREATER THAN OR EQUAL TO $branchdate"
fi
done
Test file:
01-Jan-2000
02-Feb-2007
14-Aug-2007
20-Dec-2008
Output:
01-Jan-2000 -> 20000101 LOWER THAN 20070814
02-Feb-2007 -> 20070202 LOWER THAN 20070814
14-Aug-2007 -> 20070814 GREATER THAN OR EQUAL TO 20070814
20-Dec-2008 -> 20081220 GREATER THAN OR EQUAL TO 20070814
The logic of the script is good but i don't understand what you want to do with your more|grep|awk command because the awk program in invalid.
Thank you. I should have mentioned that the awk is to read in the dates and construct another file with the date. For example, if the date was 01-Sep-2007, test the date and then configure a script that reads
time 01-Sep-2007
do something
do something
So on an so forth for the entire file. I am curious, what do you mean by the "awk" program being invalid?
The input file you created is correct. It has the dates like so
01-Sep-2006
25-Aug-2007
The file should read each date and then compare each date to the branchdate. Depending on the date, it should produce a file with the date and specific directives. For example, the first date (01-Sep-2006 converted to 20060901) is earlier than the branchdate (20070814). So it should produce a file that reads
time 01-Sep-2006
element directive_75
The next date (25-Aug-2007 converted to 20070825) should produce
time 25-Aug-2007
element directive_main
Every date prior to the branchdate should produce a specific out and the same for dates on/after the branch date. This should happen for each date in the .txt file regardless of order.