Solved

Unix ksh: awk

Posted on 2011-03-25
7
811 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Installing FreeBSD… FreeBSD is a darling of an operating system. The stability and usability make it a clear choice for servers and desktops (for the cunning). Savvy?  The Ports collection makes available every popular FOSS application and packag…
Utilizing an array to gracefully append to a list of EmailAddresses
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
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.

744 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now