Tuesday, October 2, 2012

Parse log files in WLST, print text between 2 strings

When grepping logs, it's often useful to isolate a specific string occurring between 2 other strings, like

Received exception while creating connection for pool "PIPPODataSourceJP": ORA-04031: unable to allocate 32 bytes of shared memory ("shared pool","DATABASESYS","trigger inform","kglhin: temp")
osbpr1do.log00253:ORA-04031: unable to allocate 32 bytes of shared memory ("shared pool","select obj#,type#,ctime,mtim...","sql area","tmp"

I am interested in all the strings like PIPPODataSourceJP, to extract from the logs  a unique list of Datasources failing.

Let's first grep all the lines:

grep ORA-04031 * > /tmp/allora04031.txt

this doesn't work, it prints all the line:
sed -n '/for pool /,/: ORA-04031/p' /tmp/allora04031.txt

All this sed and awk is bullshit.


import fileinput
start = 'for pool "'
end = '": ORA-04031'

for line in fileinput.input(['/tmp/allora04031.txt']):
    indstart = line.find(start, 0)
    if indstart > 0:
        indend = line.find(end, indstart)
        print line[indstart + len(start):indend]

Here is the doc http://docs.python.org/library/string.html


Anonymous said...

Or you could try this: perl -nle 'print $1 if /\s+pool\s+"(.*?)":\s+ORA-04031/' /path/to/logfile

vernetto said...

IMHO the FIRST quality of code is READABILITY. Perl, awk, sed, are all very powerful but totally unreadable unless you know them very well.