Korn shell script to get a value from logfile

16 views
Skip to first unread message

SKR

unread,
Mar 26, 2020, 3:57:12 PM3/26/20
to Korn Shell
I have a korn script to execute a oracle job to process a data file . In the shell script we are a capturing all the activities in a log file(LOG_File) and we are capturing the record count like no of gets processed and rejected(variable in log file "Num of rejected records:" ). I'd like to trigger an email to our team when the total rejected record is more than 1.

Peter Hitchman

unread,
Mar 28, 2020, 12:26:55 PM3/28/20
to SKR, Korn Shell
Hi
This is how I would code it:-

#!/usr/bin/env ksh

while read line

do

[[ ${line} =~ Number\ of\ rejected\ records:[0-9]+ ]] && IFS=: read text num < <(echo ${line})


((num > 1)) && echo "send email using mail or mailx or sendmail commands, whatever your fav command is"

done <log_file


Regards
Pete

On Thu, 26 Mar 2020 at 19:57, SKR <senth...@gmail.com> wrote:
I have a korn script to execute a oracle job to process a data file . In the shell script we are a capturing all the activities in a log file(LOG_File) and we are capturing the record count like no of gets processed and rejected(variable in log file "Num of rejected records:" ). I'd like to trigger an email to our team when the total rejected record is more than 1.

--
You received this message because you are subscribed to the Google Groups "Korn Shell" group.
To unsubscribe from this group and stop receiving emails from it, send an email to korn-shell+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/korn-shell/b2de2be2-fc68-411b-b26e-de130499f58f%40googlegroups.com.

bob desinger

unread,
Mar 28, 2020, 6:13:47 PM3/28/20
to Peter Hitchman, SKR, Korn Shell
If the log file often has more than one record per run about rejects, you could print all of the reject messages into a file. Then, after you’ve seen all of the log file, send mail outside of the while-loop — but only if there were rejects.

export rejects_file=/var/tmp/ora_rejects.$$

# . . .then the code that Pete showed,
# except that you wouldn’t mail inside the loop,
# and you’d just print or printf the reject counts to stdout.
. . .
done < log_file > $rejects_file

# mail to all of us if we saw any rejects
if [[ -s $rejects_file ]]
then
# assuming you use mailx, like Pete mentioned
mailx -s “Found rejects” -c $cc_list $us < $rejects_file
fi

rm $rejects_file

Do you have a sample line of input that you could show us? Since we’re all sitting at home bored, it would be an interesting group exercise to come up with robust code to parse it. (Bonus points if you include the code that you’re using to parse it now.)

:bd:

SKR

unread,
Mar 29, 2020, 12:22:36 PM3/29/20
to Peter Hitchman, Korn Shell
Hi Peter - Thanks for sharing it.Here do we need to give the log_file path before the while loop?
--
Regards,
Senthil.S
Reply all
Reply to author
Forward
0 new messages