Parsing Log File Based on Date & Error

I'm still up trying to figure this out and it is driving me nuts.

I have a log file which has a basic format of this...

2010-10-10 22:25:42
[Error: 43059] Init block 'UA Deployment Date': Dynamic refresh of repository scope variables has failed.
[Error: 16024] The ODBC function has returned an error. The database may not be available, or the network may be down.
2010-12-10 22:25:42
[Error: 43059] Init block 'Current Usage Accelerator Refresh Date': Dynamic refresh of repository scope variables has failed.
[Error: 16023] The ODBC function has returned an error. The database may not be available, or the network may be down.
2010-12-11 22:25:42
[Error: 43059] Init block 'New Activity Interval for Asset': Dynamic refresh of repository scope variables has failed.
[Error: 16023] The ODBC function has returned an error. The database may not be available, or the network may be down.
2010-12-11 22:25:42
[Error: 43059] Init block 'New Activity Interval for Account': Dynamic refresh of repository scope variables has failed.
[Error: 16024] The ODBC function has returned an error. The database may not be available, or the network may be down.
2010-12-13 22:25:42
[Error: 43059] Init block 'New Activity Interval for Opportunity': Dynamic refresh of repository scope variables has failed.
[Error: 16024] The ODBC function has returned an error. The database may not be available, or the network may be down.

I have a date which I return from Oracle in the exact same format as this log file in a variable called $lastdate. I was able to use grep to store the log date into $logdate.

I have to go through this logfile starting at the point where $logdate > $lastdate and only return the whole error message of error 16024 or 16023(ex: [Error: 16024] The ODBC function has returned an error. The database may not be available, or the network may be down.). I'm going nuts trying to figure out how to do the > operator between dates and then only getting the 16024 or 16023 error.

This is my code thus far which obviously doesnt work very well:

tail -f $logfile |
   while read LINE; do
    if echo $LINE | grep ^20..'-'..'-'.. >/dev/null; then
        logdate=$LINE
    echo " the log date is: ========== $logdate ============="
        if ['$logdate'<'$lastdate']; then
            if echo $LINE | grep '16024' >/dev/null; then
               echo " ========= I find the error   =================================: $LINE"
            
            #    echo $LINE | cut -d "|" -f > $tempfile
            fi
        fi
#    if echo $LINE | date >/dev/null; then
#        lastrun=$LINE
#        echo " ========= Last run date is=================================: $lastrun"
#        if [$lastrun < $lastdate ]; then
#           echo " Last run date is less than last date"
#               if echo $LINE | grep ^20..'-'..'-'.. >/dev/null; then
#                linecount=$(($linecount + 1))
#                echo " I find the line: $LINE"
#                      echo $LINE | cut -d "|" -f > $tempfile
#                         breakdown=$(echo $LINE | cut -d "]" -f 1)
#                 echo "$LINE" >> $tempfile
#                    echo "The break down is:   $breakdown" 
#            fi
        else
            echo " Last run date is larger than last date"
        fi
#    fi
exit 0
done<$logfile

Try...

 awk -v l="$lastdate" '/^....-..-../&&$0>l{d=$0}d&&/Error: 1602[34]/{print d,$0}' file1
awk -v l="$lastdate" '($0==l){n=1}(n==1)&&(/Error: 1602[34]/||/^....-/)' input

Thank you so much for these - I am very grateful. One more question. How would I store the date of the matching error in the logfile ($logdate) along with the error message?

After finding a matching error I need to Insert into an oracle table like this:

inserter=`sqlplus -s <<EOF $USERID/$PASSW@$DB
set heading off
set pagesize 0
set tab off
INSERT INTO DB.ERROR(ERR_DATE, ERR_MSG)
VALUES
($logdate,$errormsg);
exit
EOF`

thank you so much for the help though, lifesavers.

---------- Post updated at 01:12 PM ---------- Previous update was at 10:29 AM ----------

So I got this to work perfectly thanks to your help

awk -v l="$lastdate" '/^....-..-../&&$0>l{d=$0}d&&/Error: 1602[34]/{print d,$0}' $logfile

I want to put this in a DO WHILE loop though and insert the result into a variable so that I can call SQL PLUS to insert into an oracle table. So heres the syntax which i'm trying to do but doesn't seem to be working too well

while read LINE; do
DATE=`echo $LINE | awk -v l="$lastdate" '/^....-..-../&&$0>l{d=$0}d&&/Error: 1602[34]/{print d}'`
MSG=`echo $LINE | awk -v l="$lastdate" '/^....-..-../&&$0>l{d=$0}d&&/Error: 1602[34]/{print $0}'`
#here i will call sql plus and insert above variables into database
done<$logfile

Try something like...

$ export lastdate='2010-12-11 22:25:42'
$ export fmt="INSERT INTO DB.ERROR(ERR_DATE, ERR_MSG) \nVALUES \n('%s','%s');\n"
$ awk -v l="$lastdate" -v f="$fmt" '/^....-..-../&&$0>l{d=$0}d&&/Error: 1602[34]/{printf(f,d,$0)}' file1
INSERT INTO DB.ERROR(ERR_DATE, ERR_MSG)
VALUES
('2010-12-13 22:25:42','[Error: 16024] The ODBC function has returned an error. The database may not be available, or the network may be down.');
$

...redirect to a file and start in sqlplus.