Simpler Navigation for Servers and Operating Systems
Completed: a much simpler Servers and Operating Systems section of the Community. We combined many of the older boards, so you won't have to click through so many levels to get at the information you need. Check the consolidated boards here as many sub-forums are now single boards.
Languages and Scripting
cancel
Showing results for 
Search instead for 
Did you mean: 

Initiate a script for a pattern grepped in a file

SOLVED
Go to solution
zxcv
Super Advisor

Initiate a script for a pattern grepped in a file

Hi ,

 

I want to initiate a script when the following pattern is obserevd in a file.

 

For ex: 

 

file name is abclog

 

a pattern like " Start of  x1 job" comes in this logfile ;

then my scrpt called xyz.sh must run  till i get the following line in my logfile ;

"End of c2 job"

 

 

9 REPLIES
Dennis Handly
Acclaimed Contributor

Re: Initiate a script for a pattern grepped in a file

Do you want to invoke the script on the whole file but only process the lines between the Start and End?

zxcv
Super Advisor

scripting help reqd

Hi Dennis ,

Yes.

Dennis Handly
Acclaimed Contributor

Re: Initiate a script for a pattern grepped in a file

>a pattern like " Start of  x1 job" comes in this logfile ;

>till I get the following line in my logfile; "End of c2 job"

 

You can use awk (or sed) to extract out the part you want.  You could either do it all in awk or pipe it to your script:

awk '/Start of  x1 job/,/End of c2 job/ { print $0 }' abclog | your-script

zxcv
Super Advisor

Re: Initiate a script for a pattern grepped in a file

Hi Dennis,

 

its dynamically updating file i dont know the exact time of this logfile.

how can we achive in such case ?

Dennis Handly
Acclaimed Contributor

Re: Initiate a script for a pattern grepped in a file

>its dynamically updating file I don't know the exact time of this logfile.

 

I'm not sure what you mean by dynamic?  Do you mean that the End line may not be there?

Laurent Menase
Honored Contributor

Re: Initiate a script for a pattern grepped in a file

tail +1  -f abclog | awk '/Start of  x1 job/,/End of c2 job/ { print $0 }' | your-script

 

 

zxcv
Super Advisor

Re: Initiate a script for a pattern grepped in a file

Hi Dennis ,

 

Yes the file is say abclog .

it might contain any content in the day time.

And say at 9 pm mayb sudenly comes Start line and afetr 1 hr comes the END line.

Dennis Handly
Acclaimed Contributor
Solution

Re: Initiate a script for a pattern grepped in a file

>And say at 9 pm maybe suddenly comes Start line and after 1 hr comes the END line.

 

Then  Laurent's tail solution will work.  Except it will never finish and that may hang your script.

 

I suppose what you could do is  exit when you see the End:

tail +1 -f abclog | awk '

/Start of  x1 job/,/End of c2 job/ {  

   print $0

   if ($0 ~ /End of c2 job/)

      exit 0

}' | your-script

Highlighted
zxcv
Super Advisor

Re: Initiate a script for a pattern grepped in a file

Thanks Dennis