1753616 Members
5908 Online
108797 Solutions
New Discussion юеВ

Script Help ?

 
SOLVED
Go to solution
Allanm
Super Advisor

Script Help ?


I created a log search script which ssh's to other host log files and finds out errors. Here is a snippet where it cannot find the errors -

#./logsearch.sh "error-text"
================= cluster 1 host1 ===============
================= cluster 1 host2 =================
================= cluster 2 host3 =================
================= cluster 2 host4 =================

Here is a snippet of where it does find errors -

================= cluster 1 host1 =================
================= cluster 1 host2 =================
specific-errors
specific-errors
....
================= cluster 2 host3 =================
================= cluster 2 host4 =================


I want to create a script which identifies which cluster-host combination has those specific error and then page out to the team with that information specifying cluster-host(which has the errors) in that page.
How do I parse the cluster-host combination which has issues from this output is my question through bash.
4 REPLIES 4
James R. Ferguson
Acclaimed Contributor
Solution

Re: Script Help ?

Hi:

Try this:

# awk '/^===/ {PREV=$0};{if (/error/) {print PREV;print}}' file

You may need to change the trigger "error" to something else.

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: Script Help ?

Hi (again) Allanm:

This variation yields cleaner output:

# awk '/^===/ {PREV=$0};{if (/error/) {if (PREV) {print PREV;PREV="";print} else {print}}}' file

Regards!

...JRF...
Allanm
Super Advisor

Re: Script Help ?

Thanks JRF!

The following actually worked for me -

awk '/^===/ {PREV=$0};{if (/error/) {print PREV}}' log

If you can explain a little more abt it as well that will be great.

Allan.
James R. Ferguson
Acclaimed Contributor

Re: Script Help ?

Hi (again) Allanm:

> # awk '/^===/ {PREV=$0};{if (/error/) {print PREV;print}}'

> If you can explain a little more abt it as well that will be great.

As each line from your file is read, a test is made to see if the line begins (^) with the characters '==='. If if does, the statement in braces is executed. That is, the whole line ($0) is captured in the 'PREV' variable. A test is then made for lines with the sequence of 'error'. If that test is true, then the 'PREV' variable contents is printed followed by the line just read. In 'awk', a 'print' without any object is a print of the current line (which can also be explicitly written as '$0').

Regards!

...JRF...