Extract repeating and check for for certain error message

Hi guys

I have an log from an regular application processing where the processing is done every 10 minutes and is written to the same log which is rotated on daily basis.

the block of log starts always with "Starting" and ends with a certain pattern "Completed"

The catch is I want to create an shell script for checks if an Oracle ORA error is found in this log block and if yes this is collected and send after whole log was checked to several recipients.

I have an idea how to do the whole thing what I have an issue is the part of script which checks in the log block for the ORA message

how does the log look like:

Starting
14:10:*
14:10:* some stuff
14:10:*
14:10:* blabla
Completed


Starting
14:20:*
14:20:* some stuff
14:20:* ORA- error constaint voilated
14:20:* blabla
Completed

Starting
14:30:*
14:30:* some stuff
14:30:* blabla
14:30:* blabla
Completed

Starting
14:40:*
14:40:* some stuff
14:40:* ORA- error constaint voilated
14:40:* blabla
Completed

Starting
14:50:*
14:50:* some stuff
14:50:* blabla
14:530:* blabla
Completed

the final report should contain in the blocks

Starting
14:20:*
14:20:* some stuff
14:20:* ORA- error constaint voilated
14:20:* blabla
Completed

Starting
14:40:*
14:40:* some stuff
14:40:* ORA- error constaint voilated
14:40:* blabla
Completed

keep in mind that the number of lines between Starting and Completed isnt fixed but may change

the bocks can be extracted like this: sed -n '/StartPattern/,/EndPattern/p' FileName
$ awk '/abc/{flag=1;next}/mno/{flag=0}flag' file

now I could do the grep -A50 -B50 'ORA-' file >> report but this would extract more what I like, I want to make the report clean.

One idea was maybe to get the Line number when the error ORA appers and then do an row-1 until Starting is found and row+1 until the Completed is found and get those line numbers and to an sed -n 'X,Yp' file

Any suggestion thank you in advance.

Martin

how about:

awk '/^Starting.*Completed$/ && /ORA- error/' RS=  ORS='\n\n' myFile
1 Like

If you're only processing completed log files, vgersh99's suggestion can be simplified a little bit:

awk '/ORA- error/' RS=  ORS='\n\n' myFile

If you are processing active log files and you want to be sure that the output only contains complete entries, you could still simplify it slightly:

awk '/Completed$/ && /ORA- error/' RS=  ORS='\n\n' myFile

If the trailing empty line at the end of the output produced by vgersh99's suggestion and the above two modifications bother you, you could also try:

awk '/ORA- error/{if(c++)print "";print}' RS= myFile

or:

awk '/Completed$/ && /ORA- error/{if(c++)print "";print}' RS= myFile
1 Like

Hi

thank you so much :slight_smile:

awk '/ORA- error/' RS=  ORS='\n\n' myFile 

and also

awk '/^Starting.*Completed$/ && /ORA- error/' RS=  ORS='\n\n' myFile 

does the trick pretty good but here

awk '/^Starting.*Completed$/ && /ORA- error/' RS=  ORS='\n\n' myFile

I have to remove the ^Starting and Completed $ since they are not the first/last things in the line

Hi guys

is is there a possibility to include the filename in the resulted report only for the log name if there contain such a lines ? thank you

will use the code block sorry about that

awk '/ORA- error/{print FILENAME}' RS= myFile

cool one step closer:

I want something like this

### ORA found in log: /app/logs/processing1.log
Starting
14:20:*
14:20:* some stuff
14:20:* ORA- error constaint voilated
14:20:* blabla
Completed

### ORA found in log: /app/logs/processing5.log
Starting
14:40:*
14:40:* some stuff
14:40:* ORA- error constaint voilated
14:40:* blabla
Completed
awk '/ORA- error/{print "### ORA found in log: " FILENAME, $0 }' RS= ORS='\n\n' myFile
1 Like