Initiate a script for a pattern grepped in a file (415 Views)
Reply
Super Advisor
zxcv
Posts: 226
Registered: ‎04-09-2010
Message 1 of 10 (415 Views)
Accepted Solution

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"

 

 

Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 24,965
Registered: ‎03-06-2006
Message 2 of 10 (405 Views)

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?

Please use plain text.
Super Advisor
zxcv
Posts: 226
Registered: ‎04-09-2010
Message 3 of 10 (402 Views)

scripting help reqd

Hi Dennis ,

Yes.

Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 24,965
Registered: ‎03-06-2006
Message 4 of 10 (400 Views)

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

Please use plain text.
Super Advisor
zxcv
Posts: 226
Registered: ‎04-09-2010
Message 5 of 10 (396 Views)

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 ?

Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 24,965
Registered: ‎03-06-2006
Message 6 of 10 (394 Views)

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?

Please use plain text.
Honored Contributor
Laurent Menase
Posts: 1,079
Registered: ‎11-06-2003
Message 7 of 10 (390 Views)

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

 

 

Please use plain text.
Super Advisor
zxcv
Posts: 226
Registered: ‎04-09-2010
Message 8 of 10 (370 Views)

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.

Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 24,965
Registered: ‎03-06-2006
Message 9 of 10 (368 Views)

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

Please use plain text.
Super Advisor
zxcv
Posts: 226
Registered: ‎04-09-2010
Message 10 of 10 (336 Views)

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

Thanks Dennis
Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation