Solved

Unix ksh: awk

Posted on 2011-03-25
7
813 Views
Last Modified: 2012-06-21
Dear Expert,

I need to distinguish between these two cases:

1) abend (ABEND) after success (SUCC) => action1

 >>every run  ALEIDOCFAILCHECKEMAIL-028A   SUCC  10 04:02  00:03           0 #J29703
 >>every run  ALEIDOCFAILCHECKEMAIL-028A   ABEND 10 04:16  00:01           1 #J10616; <07:50

2) abend after abend => action2

 >>every run  FIGL-IE-D-0964-CITI_MT940    ABEND 10 02:29  00:02           1 #J28862
 >>every run  FIGL-IE-D-0964-CITI_MT940    ABEND  0 02:49  00:02           1 #J12900; &0:20

The ">> every run" lines are in 'inputfile'.
So what is needed here is the syntax to read the inputfile and if last line is ABEND with the entry above SUCC => action1, and if last line is ABEND with entry above also ABEND => action2.

Thank you for very much for your highly appreciated help.
0
Comment
Question by:Watnog
  • 3
  • 2
  • 2
7 Comments
 
LVL 4

Expert Comment

by:bazika
ID: 35213696
I think it could be something like this:
cat your_input_file | awk '
BEGIN { s_prev="nothing" ; s_cur="nothing"}
/every run/ { s_prev = s_cur ; s_cur = $0 }
END { 
 if (s_prev~/ABEND/ && s_cur ~ /SUCC/ ) { ... your action 1 ...}
 if (s_prev~/ABEND/ && s_cur ~ /ABEND/ ) { ... your action 2 ...}
' 

Open in new window

0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 35213711
Hi again,

can "ABEND" or "SUCC" appear elsewhere in the file?

If not, please run as a first try the below one.

It will print the respective second line followed by "action1" or "action2" so you can see if this is the result you desire.
We can later actually execute the actions instead of displaying, but first have a look!

awk '{if($0~"SUCC")
     {getline; if($0~"ABEND") print $0,"action1"}
      if($0~"ABEND")
       {getline; if($0~"ABEND") print $0,"action2"}}' inputfile

wmp
0
 
LVL 4

Expert Comment

by:bazika
ID: 35213712
Here in the "BEGIN" line the script creates 2 variables.
If the string contains "every run" string, the values of these variables are changed: the previous value is "shifted" to the s_prev, and the current value ($0) is assigned to s_cur.
After the end of the file (END directive) the values of the variables are analyzed and Your actions are executed if necessary.

This is, of course, only the draft template.
0
ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 

Author Comment

by:Watnog
ID: 35213882
Thank you both.
An issue with both proposals, and not included in my question, is that indeed there can be multiple lines.
For this only the last 2 lines would matter (tail -2), but by that tailing I could in fact have the inputfile containing just those two last lines.
In that case Wmp your syntax would do.
This is the output:

 >>every run                                 FIGL-IE-D-0964-CITI_MT940                ABEND  0 02:49  00:02           1 #J12900; &0:20 action2

Remains to have action2 become an actual cmd.

Something asked on top of this...
If I were to [tail -3] so the input file would have 3 lines, and the last line with ABEND would need to have 2 lines with ABEND above it in order for action2 to be executed, like this:

>>every run    FIGL-IE-D-0964-CITI_MT940     ABEND   0 02:49  00:02           1 #J34258
>>every run    FIGL-IE-D-0964-CITI_MT940     ABEND 10 02:29  00:02           1 #J28862
>>every run    FIGL-IE-D-0964-CITI_MT940     ABEND   0 02:49  00:02           1 #J12900

Can this be done similarly?
Hope this is clear...


0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 35214010
OK,

what should this actual cmd be?

I put a "system" call with "echo" into the script. Replace it with whatever command you desire.
Mind the quotes! Escaped quotes are "seen" by the command, unescaped ones are seen by awk.

awk '
    {if($0~"SUCC")
     {getline; if($0~"ABEND") system("echo \"Action 1 required on line "NR"\"")}
      if($0~"ABEND")
       {getline; if($0~"ABEND")  system("echo \"Action 2 required on line "NR"\"")}}' inputfile

And now for your second requirement. I left the "SUCC" part as it was and added a check for a third "ABEND" line:

awk '
    {if($0~"SUCC")
     {getline; if($0~"ABEND") system("echo \"Action 1 required on line "NR"\"")}
      if($0~"ABEND")
       {getline; if($0~"ABEND")
         {getline; if($0~"ABEND") system("echo \"Action 3 required on line "NR"\"")}}}' inputfile

wmp

0
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 500 total points
ID: 35214105
We can combine the "2 ABENDs" and "three ABENDs" versions:

awk '
    {if($0~"SUCC")
     {getline; if($0~"ABEND") system("echo \"Action 1 required on line "NR"\"")}
      if($0~"ABEND")
       {getline; if($0~"ABEND")
        {getline; if($0~"ABEND") system("echo \"Action 3 required on line "NR"\"")
           else system("echo \"Action 2 required on line "NR-1"\"")}}}'  inputfile

Action1 is for "SUCC" then "ABEND"
Action2 is for "ABEND" then "ABEND" then not "ABEND"
Action3 is for "ABEND" then "ABEND" then "ABEND"

wmp
0
 

Author Closing Comment

by:Watnog
ID: 35214323
Amazing.
Thanks a million.
0

Featured Post

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Convert curl speed_dwonload to Mbps 29 115
How to clone solaris 10 machine 33 131
How to calculate the current use of PVU on my TSM server. 4 73
centos commands 6 53
Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap (http://www.tcpdump.org) Version 1.2 2.      Jpcap(http://netresearch.ics.uci.edu/kfujii/Jpcap/doc/index.html) Version 0.6 Prerequisite: 1.      GCC …
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question