Languages and Scripting
Showing results for 
Search instead for 
Do you mean 

Initiate a script for a pattern grepped in a file

SOLVED
Go to Solution
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"

 

 

1 ACCEPTED SOLUTIONS
Acclaimed Contributor Acclaimed Contributor

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

[ Edited ]

>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

9 REPLIES
Acclaimed Contributor 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?

Super Advisor

scripting help reqd

Hi Dennis ,

Yes.

Acclaimed Contributor 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

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 ?

Acclaimed Contributor 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?

Honored Contributor 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

 

 

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.

Acclaimed Contributor Acclaimed Contributor

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

[ Edited ]

>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
Super Advisor

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

Thanks Dennis