Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Unix ksh: awk

Posted on 2011-03-25
7
Medium Priority
?
818 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
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 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

972 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