Pulling x number of lines from one file and save into another

Hi,

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.

Cheers

Try:

awk '/^SQL/{s=$0;next}{s=s RS $0} /^ERROR/{f=1} f && /^Elapsed/{print s; f=0}' file
1 Like

Hi

I added the above to my script, however it didn't do anything (i.e. no ERRORS within my new log file, however there are errors in update.logs).

The script doesn't error, just hangs at command line - your scrpt is the last bit of code within my script :slight_smile:

#!/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

1 Like

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
$ 
1 Like