Solved

Unix ksh: awk

Posted on 2011-03-25
7
812 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
retrieve nuemaric value from txt file in shell script 3 61
unix in java example 9 53
unix example issues 18 76
Powershell to pull all Service accounts 1 47
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…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
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.

863 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

24 Experts available now in Live!

Get 1:1 Help Now