scripting help reqd (662 Views)
Reply
Super Advisor
zxcv
Posts: 226
Registered: ‎04-09-2010
Message 1 of 15 (662 Views)
Accepted Solution

scripting help reqd

hi ,

 

Need a help in getting a formated o/p ;

 

 

*** Start of IN635N1 at Tue Sep 25 23:12:19 IST 2012
*** End of IN635N1 at Tue Sep 25 23:12:20 IST 2012 - RC = 0
*** Start of IN9701 at Tue Sep 25 23:12:24 IST 2012
*** End of IN9701 at Tue Sep 25 23:12:26 IST 2012 - RC = 0

I need to grep RC codes from the above file and it must mail me immediately 

Honored Contributor
Matti_Kurkela
Posts: 6,271
Registered: ‎12-02-2001
Message 2 of 15 (655 Views)

Re: scripting help reqd

#!/bin/sh
SOURCEFILE=/some/file
DESTINATION=zxcv@company.example
SUBJECT="Your RC report"

(printf "%8s %s\n" "Job" "RC"; \
echo "------------"; \ grep -- "- RC =" <"$SOURCEFILE" \ | awk '{printf "%8s %s\n",$4,$15}') \
| mailx -s "$SUBJECT" "$DESTINATION"

 

You specified you want formatted output, but you did not specify the format of the result you wanted.

The above script will produce a mail that will look like this:

 

To: zxcv@company.example
Subject: Your RC report

     Job RC
------------
 IN635N1 0
  IN9701 0

 If this is not what you wanted, please specify the output format you want.

 

MK
Super Advisor
zxcv
Posts: 226
Registered: ‎04-09-2010
Message 3 of 15 (653 Views)

Re: scripting help reqd

Hi Matti ,

My o/p file attched herewith.

I want o/p as ;

 

It muxt mail List of job name only other than RC = 0.

 

 

For ex:  

 

IF9591A RC= number ( other than zero )

 

Super Advisor
zxcv
Posts: 226
Registered: ‎04-09-2010
Message 4 of 15 (652 Views)

Re: scripting help reqd

PFA o/p file.

 

Super Advisor
zxcv
Posts: 226
Registered: ‎04-09-2010
Message 5 of 15 (651 Views)

Re: scripting help reqd

Hi Matti ,

 

Its an online file which gets generated daily from 9pm to 9 am.

How can we grep other than RC = 0 from it and mail it immediately.

Acclaimed Contributor
Dennis Handly
Posts: 25,274
Registered: ‎03-06-2006
Message 6 of 15 (642 Views)

Re: scripting help required

>How can we grep other than RC = 0 from it and mail it immediately.

 

You could do something like:

awk '

BEGIN { start="No previous Start line" }

/Start of/ {

   start = $0 # save

   next

}

/End of/ {

   if ($NF != 0) {  # if RC != 0, print start and stop lines

      print start

      print $0

   }

   next

}' file-name > failures.log

if [ -s failures.log ]; then

   mailx -s "subject" zxcv@company.example < failures.log

fi

rm -f failures.log

Super Advisor
zxcv
Posts: 226
Registered: ‎04-09-2010
Message 7 of 15 (622 Views)

Re: scripting help required

Hi Dennis ,

 

The o/p is fine.

But say that file gets generated form 9 pm .

I need to put this script in cron right ?

 

It must be run when the input file is getting generated.

Acclaimed Contributor
Dennis Handly
Posts: 25,274
Registered: ‎03-06-2006
Message 8 of 15 (616 Views)

Re: scripting help required

>I need to put this script in cron right?   It must be run when the input file is getting generated.

 

Yes, you could put it in a crontab.  This would mean you don't have to change how the logfile is generated.

Or you could add to the current script to scan and mail it after the file is finished.

Super Advisor
zxcv
Posts: 226
Registered: ‎04-09-2010
Message 9 of 15 (610 Views)

Re: scripting help required

Hi Dennis ,

 

One small query , what if i want to grep after this line ;

*** Start of shutcons at Fri Sep 21 21:27:43 IST 2012

 

 

 

and before this line whatevr comes in between.
*** End of CI0251 at Sat Sep 22 03:37:00 IST 2012 - RC = 0

Acclaimed Contributor
Dennis Handly
Posts: 25,274
Registered: ‎03-06-2006
Message 10 of 15 (602 Views)

Re: scripting help required

[ Edited ]

>what if I want to grep after this line   ...

 

Is this a separate requirement or did you want both at the same time?

If only between those two lines:

awk  '
/Start of shutcons at Fri Sep 21 21:27:43 IST 2012/, \
/End of CI0251 at Sat Sep 22 03:37:00 IST 2012/ {
   print
} ' file-name

 

If it is that simple, you could also use sed.

Super Advisor
zxcv
Posts: 226
Registered: ‎04-09-2010
Message 11 of 15 (585 Views)

Help reqd for scripting

Hi Dennis , 

My reqmnt is as follows;

 

a logfile gets generated at 9 pm say abc.log which contains a line ;

 

Start of shutcons 

 

i want to capture RC codes for all jobs afetr this line  , and it must fire a mail grepping RC other than zero codes , till i get a line given below in my dynamically generating log file  ;

 

End of CI0251

 

Sample of abc.log file is afollows ;

 

Start of shutcons at  Tue Sep 25 21:28:40 IST 2012

 

*** End   of shutcons at  Tue Sep 25 21:28:40 IST 2012  - RC =  0

*** Start of  SY9600  at  Tue Sep 25 21:29:05 IST 2012

*** End   of  SY9600  at  Tue Sep 25 21:29:05 IST 2012  - RC =  0

*** Start of shutcons at  Tue Sep 25 21:29:06 IST 2012

 

*** End   of shutcons at  Tue Sep 25 21:29:06 IST 2012  - RC =  0

*** Start of hksh0004 at  Tue Sep 25 21:29:12 IST 2012

 

*** End   of hksh0004 at  Tue Sep 25 21:29:12 IST 2012  - RC =  0

*** Start of shutbtm at  Tue Sep 25 21:29:19 IST 2012

 

*** End   of shutbtm at  Tue Sep 25 21:29:19 IST 2012  - RC =  0

*** Start of stopgateway at  Tue Sep 25 21:29:37 IST 2012

 

*** End   of stopgateway at  Tue Sep 25 21:29:38 IST 2012  - RC =  0

*** Start of shutbtm at  Tue Sep 25 21:29:40 IST 2012

 

*** End   of shutbtm at  Tue Sep 25 21:29:40 IST 2012  - RC =  0

*** Start of  UTDEBUG1  at  Tue Sep 25 21:29:41 IST 2012

*** End   of  UTDEBUG1  at  Tue Sep 25 21:29:41 IST 2012  - RC =  0

*** Start of hkcheckdf at  Tue Sep 25 21:29:43 IST 2012

 

*** End   of hkcheckdf at  Tue Sep 25 21:29:43 IST 2012  - RC =  0

*** Start of chkrerun at  Tue Sep 25 21:29:47 IST 2012

 

*** End   of chkrerun at  Tue Sep 25 21:29:47 IST 2012  - RC =  0

*** Start of  SY0007  at  Tue Sep 25 21:29:53 IST 2012

*** End   of  SY0007  at  Tue Sep 25 21:29:54 IST 2012  - RC =  0

*** Start of  SY3100  at  Tue Sep 25 21:30:12 IST 2012

*** End   of  SY3100  at  Tue Sep 25 21:30:12 IST 2012  - RC =  0

*** Start of  SP0100  at  Tue Sep 25 21:30:13 IST 2012

*** End   of  SP0100  at  Tue Sep 25 21:30:35 IST 2012  - RC =  0

*** Start of spms0122 at  Tue Sep 25 21:30:36 IST 2012

 

*** End   of spms0122 at  Tue Sep 25 21:31:07 IST 2012  - RC =  0

*** Start of  SY9507  at  Tue Sep 25 21:31:08 IST 2012

*** End   of  SY9507  at  Tue Sep 25 21:31:11 IST 2012  - RC =  0

*** Start of stopgateway at  Tue Sep 25 21:31:16 IST 2012

 

*** End   of stopgateway at  Tue Sep 25 21:31:16 IST 2012  - RC =  0

*** Start of shutbtm at  Tue Sep 25 21:31:17 IST 2012

 

*** End   of shutbtm at  Tue Sep 25 21:31:17 IST 2012  - RC =  0

*** Start of  SY9600  at  Tue Sep 25 21:31:18 IST 2012

*** End   of  SY9600  at  Tue Sep 25 21:31:18 IST 2012  - RC =  0

*** Start of  UTDEBUG1  at  Tue Sep 25 21:31:19 IST 2012

*** End   of  UTDEBUG1  at  Tue Sep 25 21:31:19 IST 2012  - RC =  0

*** Start of  SY0007  at  Tue Sep 25 21:31:27 IST 2012

*** End   of  SY0007  at  Tue Sep 25 21:31:27 IST 2012  - RC =  04

*** Start of  SY3300  at  Tue Sep 25 21:31:47 IST 2012

*** End   of  SY3300  at  Tue Sep 25 21:31:47 IST 2012  - RC =  0

*** Start of  SY0199  at  Tue Sep 25 21:31:56 IST 2012

*** End   of  SY0199  at  Tue Sep 25 21:31:56 IST 2012  - RC =  254

 

End of CI0251

 

It must fire a mail saying job sy0199 RC = 254

It must fire a mail saying job sy0007 RC = 04 immediately as it comes in abc.log file.

Acclaimed Contributor
Dennis Handly
Posts: 25,274
Registered: ‎03-06-2006
Message 12 of 15 (576 Views)

Re: Help reqd for scripting

[ Edited ]

awk  '
/Start of shutcons/, /End of CI0251/ {
   if ($2 == "End" && $3 == "of" && $NF != 0) {
      # if RC != 0, print stop line
      print "job", $4, $(NF-2), "=", $NF
   }
} ' abc.log > failures.log

if [ -s failures.log ]; then

   mailx -s "subject" zxcv@company.example < failures.log

fi

rm -f failures.log



Super Advisor
zxcv
Posts: 226
Registered: ‎04-09-2010
Message 13 of 15 (571 Views)

Re: Help reqd for scripting

Hi ,

 

Dennis ,

 

i tried the above script , but was not able to grep RC code 12 from the below lines;

 

*** Start of shutcons at Wed Oct 10 00:30:17 IST 2012

*** End of shutcons at Wed Oct 10 00:30:18 IST 2012 - RC = 0

*** End of SY0331 at Wed Oct 10 01:23:17 IST 2012 - RC = 0
*** Start of BR2490 at Wed Oct 10 01:23:23 IST 2012
*** End of BR2490 at Wed Oct 10 01:23:36 IST 2012 - RC = 0
*** Start of cfps0201 at Wed Oct 10 01:23:38 IST 2012

*** End of cfps0201 at Wed Oct 10 01:23:38 IST 2012 - RC = 0
*** Start of SY0201 at Wed Oct 10 01:23:39 IST 2012
*** End of SY0201 at Wed Oct 10 01:23:39 IST 2012 - RC = 12
*** Start of IN0787 at Wed Oct 10 01:23:40 IST 2012
*** End of IN0787 at Wed Oct 10 01:23:40 IST 2012 - RC = 0
*** Start of CI0251 at Wed Oct 10 01:23:43 IST 2012
*** End of CI0251 at Wed Oct 10 01:23:52 IST 2012 - RC = 0

 

Acclaimed Contributor
Dennis Handly
Posts: 25,274
Registered: ‎03-06-2006
Message 14 of 15 (563 Views)

Re: Help reqd for scripting

>I tried the above script , but was not able to grep RC code 12 from the below lines;

 

*** Start of shutcons at Wed Oct 10 00:30:17 IST 2012

...

*** End of CI0251 at Wed Oct 10 01:23:52 IST 2012 - RC = 0

 

From your example file, I assumed that the start and ending strings didn't have leading "***".  So change to:

/Start of shutcons/, /End of CI0251/ {

  

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

Re: Help reqd for scripting

Hi Dennis ,

 

Thanks very much.

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.