I have a log file (updates.log), and I want to hunt the file for any errors. Here's an example of the log file:
SQL> update <table1> set <value1> = '*****';
update <table1> set <value1> = '*****'
*
ERROR at line 1:
ORA-00942: table or view does not exist
Elapsed: 00:00:00.00
SQL> update <table2> set <value2> = '*****';
update <table2> set <value2> = '*****'
*
ERROR at line 1:
ORA-00942: table or view does not exist
Elapsed: 00:00:00.00
I'd like to be able to pull out the 8 rows from SQL> to Elapsed, but not sure how to do this. ATM I'm grepping the file looking for ERROR, but the result that I'm given is more or less useless (ERROR at line 1). I only want to pull out these 8 lines if there's an ERROR.
Any idea how I can acheive this? Have taken a look through this forum but haven't managed to find a solution to fit this - any feedback/ thoughts appreciated.
#!/bin/bash
#tested with bash 4
content=$(<file)
while [[ $content =~ Elapsed ]]
do
c=${content#*Elapsed}
case "${content%%Elapsed*}" in
*SQL*ERROR*)
echo "${content%%Elapsed*}"
;;
esac
content="$c"
done
With an example of your input file I get this output:
$ cat file
Other line
Other line
Other line
SQL> update <table1> set <value1> = '*****';
update <table1> set <value1> = '*****'
*
ERROR at line 1: FIRST ERROR
ORA-00942: table or view does not exist
Elapsed: 00:00:00.00
Other line
Other line
Other line
SQL> update <table2> set <value2> = '*****';
update <table2> set <value2> = '*****'
*
ERROR at line 1: SECOND ERROR
ORA-00942: table or view does not exist
Elapsed: 00:00:00.00
Other line
Other line
Other line
$
$ awk '/^SQL/{s=$0;next}{s=s RS $0} /^ERROR/{f=1} f && /^Elapsed/{print s; f=0}' file
SQL> update <table1> set <value1> = '*****';
update <table1> set <value1> = '*****'
*
ERROR at line 1: FIRST ERROR
ORA-00942: table or view does not exist
Elapsed: 00:00:00.00
SQL> update <table2> set <value2> = '*****';
update <table2> set <value2> = '*****'
*
ERROR at line 1: SECOND ERROR
ORA-00942: table or view does not exist
Elapsed: 00:00:00.00
$