How to capture the cause of the exception using unix shell?

Hi all,

Can u pls guide me in the following requirement,

Am capturing the exceptions and the cause of that exception from a log file.

i cud capture the exceptions by simple grep ie., with the following command

cat logfile | grep "Exceptions"

But i couldn't able to capture the cause of that exception. this cause of the exception line can be identified by a pattern "msg=" which occurs before that "Exceptions" occurence.

The thing is if i put `egrep "Exceptions" | "msg="` then it finds all the occurences of "msg=" which is indeed not necessary. i need only the "msg=" occurence line(along with the exceptions) which is found before the "Exceptions" pattern line.

Thanks in Advance!! Please help on this...

please provide the snippet of your log file, and also point out the expected output.

Thanks for ur response!

Log file will look like,

06-16 23:55:04,INFO,ids*************************sometext,msg=curl -H "cause of the exception"
06-16 23:55:05,WARN,,,,ids*************************sometext Exception: ids*************************sometext
06-16 23:55:04,INFO,,,,ids*************************sometext
06-16 23:55:04,INFO,ids*************************sometext,msg=curl -H "cause of the exception"
06-16 23:55:05,INFO,,,,ids*************************sometext
06-16 23:55:04,INFO,,,,ids*************************sometext
06-16 23:55:05,WARN,,,,ids*************************sometext Exception: ids*************************sometext
06-16 23:55:05,WARN,,,,ids*************************sometext Exception: ids*************************sometext
06-16 23:55:05,ERROR,,,,ids*************************sometext Exception: ids*************************sometext

output file should be like,

06-16 23:55:04,INFO,ids*************************sometext,msg=curl -H "cause of the exception"
06-16 23:55:04,WARN,,,,ids*************************sometext Exception: ids*************************sometext
06-16 23:55:04,INFO,ids*************************sometext,msg=curl -H "cause of the exception"
06-16 23:55:05,WARN,,,,ids*************************sometext Exception: ids*************************sometext
06-16 23:55:05,WARN,,,,ids*************************sometext Exception: ids*************************sometext
06-16 23:55:05,ERROR,,,,ids*************************sometext Exception: ids*************************sometext

does this help?

awk '/msg=/{m=$0} /Exception/{ if (m)print m; print $0; m=0}' yourLog.txt
1 Like

Hi Thanks.. this code is working for me!! can u pls explain this code...

if your grep support -A and -B then you can try this.

A - after
B - before

 
grep -B 1 "Exception" filename
 
awk '/msg=/{m=$0} /Exception/{ if (m)print m; print $0; m=0}' yourLog.txt

if the line has

word then

is storing the whole line into the variable called

then we are checking the line has the word

and if the line has Exception and if the variable m also not empty then print the line and make the msg variable to zero.

1 Like

Hi Just a quick clarification,

if my exception tracking words may be "ERROR" or "Exception" or "exception".

Then shall i use it like,

awk '/msg=/{m=$0} /exception|ERROR|Exception/{ if (m)print m; print $0; m=0}' >> out3.xls yourLog.txt

Thanks!

 
/exception|ERROR|Exception/
 
to 
 
/exception/ || /ERROR/ || /Exception/

I tried this, but the line with "msg=" appears twice in the output file like,

06-16 23:55:04,INFO,ids*************************sometext,msg=curl -H "cause of the exception"
06-16 23:55:04,INFO,ids*************************sometext,msg=curl -H "cause of the exception"
06-16 23:55:05,WARN,,,,ids*************************sometext Exception: ids*************************sometext
06-16 23:55:04,INFO,ids*************************sometext,msg=curl -H "cause of the exception"
06-16 23:55:04,INFO,ids*************************sometext,msg=curl -H "cause of the exception"
06-16 23:55:05,WARN,,,,ids*************************sometext Exception: ids*************************sometext
06-16 23:55:05,WARN,,,,ids*************************sometext Exception: ids*************************sometext
06-16 23:55:05,ERROR,,,,ids*************************sometext Exception: ids*************************sometext

---------- Post updated at 06:43 AM ---------- Previous update was at 06:04 AM ----------

Got it thanks...

Its not case sensitive thats y we r getting it twice.