Re: Initiate a script for a pattern grepped in a file (426 Views)
Reply
Super Advisor
zxcv
Posts: 226
Registered: ‎04-09-2010
Message 1 of 10 (505 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"

 

 

Acclaimed Contributor
Dennis Handly
Posts: 25,272
Registered: ‎03-06-2006
Message 2 of 10 (495 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?

Super Advisor
zxcv
Posts: 226
Registered: ‎04-09-2010
Message 3 of 10 (492 Views)

scripting help reqd

Hi Dennis ,

Yes.

Acclaimed Contributor
Dennis Handly
Posts: 25,272
Registered: ‎03-06-2006
Message 4 of 10 (490 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

Super Advisor
zxcv
Posts: 226
Registered: ‎04-09-2010
Message 5 of 10 (486 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 ?

Acclaimed Contributor
Dennis Handly
Posts: 25,272
Registered: ‎03-06-2006
Message 6 of 10 (484 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?

Honored Contributor
Laurent Menase
Posts: 1,079
Registered: ‎11-06-2003
Message 7 of 10 (480 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

 

 

Super Advisor
zxcv
Posts: 226
Registered: ‎04-09-2010
Message 8 of 10 (460 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.

Acclaimed Contributor
Dennis Handly
Posts: 25,272
Registered: ‎03-06-2006
Message 9 of 10 (458 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

Super Advisor
zxcv
Posts: 226
Registered: ‎04-09-2010
Message 10 of 10 (426 Views)

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

Thanks Dennis
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.