Solved

Unix ksh: awk

Posted on 2011-03-25
7
815 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

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…
Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

734 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