awk script to match string and print status

Dear team,

Need support to built awk script for below requirement

Input file

LOTC cluster state:
-------------------
Node safNode=SC_2_1 joined cluster | 
Node safNode=SC_2_2 joined cluster | 
Node safNode=PL_2_3 fail cluster | 

AMF cluster state:
------------------
saAmfNodeAdminState."safAmfNode=SC-1,safAmfCluster=myAmfCluster": Unlocked
saAmfNodeOperState."safAmfNode=SC-1,safAmfCluster=myAmfCluster": Enabled
saAmfNodeAdminState."safAmfNode=SC-2,safAmfCluster=myAmfCluster": locked
saAmfNodeOperState."safAmfNode=SC-1,safAmfCluster=myAmfCluster": disabled

Here from this input file need if "LOTC cluster state:" has status other than "joined" i.e fail then need to print "LOTC Cluster status is not ok"
and
if "AMF cluster state:" has status other than "unlocked and enabled" in last column than need to print "AMF cluster state: is not ok".

The number of lines in input file for both titles can vary in numbers.

Hello shanul karim,

Sorry but this is NOT the way UNIX.com forums work. We do encourage people to add their efforts which they have put in order to solve their own problems, so please do add your efforts in your question and let us know then.

I am also adding tag to make this Amber color once you add efforts that will be reverted then.

Thanks,
R. Singh

awk 'NF>1 && /LOTC cluster state:/{print "lotc cluster"}/Node/{ if ($3!="joined") print "LOTC cluster state is not ok"; next}' sample.txt

tried the above code

but response not as expected

lotc cluster
LOTC cluster state is not ok
LOTC cluster state is not ok
LOTC cluster state is not ok
LOTC cluster state is not ok
LOTC cluster state is not ok
LOTC cluster state is not ok
1 Like

You're not too far off for the first part. Try this small adaption of your attempt (and its AMF extension, btw):

awk '

/cluster state:/        {print $1, $2
                         OK[$1] = 1
                         getline
                         next
                        }

/Node/ && OK["LOTC"]    {if ($3 != "joined")    {print "LOTC cluster state is not ok"
                                                 OK["LOTC"] = 0
                                                }
                        }
OK["AMF"]               {if ("UNLOCKED ENABLED" !~ toupper($NF))        {print "AMF cluster state: is not ok"
                                                                         OK["AMF"] = 0
                                                                        }
                        }
' file
LOTC cluster
LOTC cluster state is not ok
AMF cluster
AMF cluster state: is not ok
1 Like

Thanks a lot RudiC

Its working fine except the reverse condition . In case we have all "LOTC cluster state" as joined (without fail status) and AMF cluster state: as all enebled and unlocked status.

LOTC cluster state:
-------------------
Node safNode=SC_2_1 joined cluster | Wed Dec 5 17:03:36 2018
Node safNode=SC_2_2 joined cluster | Wed Dec 5 17:03:36 2018
Node safNode=PL_2_3 joined cluster | Wed Dec 5 17:04:34 2018

AMF cluster state:
------------------
saAmfNodeAdminState."safAmfNode=SC-1,safAmfCluster=myAmfCluster": Unlocked
saAmfNodeOperState."safAmfNode=SC-1,safAmfCluster=myAmfCluster": Enabled
saAmfNodeAdminState."safAmfNode=SC-2,safAmfCluster=myAmfCluster": locked
saAmfNodeOperState."safAmfNode=SC-1,safAmfCluster=myAmfCluster": Enabled

Here output should be

LOTC cluster
LOTC cluster state is ok
AMF cluster
AMF cluster state: is ok

instead which is coming as below

LOTC cluster
AMF cluster
LOTC cluster state is not ok
AMF cluster state: is not ok

--- Post updated at 09:53 AM ---

Dear RuDC,

Changed the code but still need improvement in AMF part.

$ awk '
>
> /cluster state:/        {print $1, $2
>                          OK[$1] = 1
>                          getline
>                          next
>                         }
>
> /Node/ && OK["LOTC"]    {if ($3 != "joined")    {print "LOTC cluster state is not ok"
>                                                  OK["LOTC"] = 0
>                                                 } else {print "LOTC cluster state is ok"
>                                                  OK["LOTC"] = 0
>                                                 }
>                         }
> OK["AMF"]               {if ("UNLOCKED ENABLED" !~ toupper($NF))        {print "AMF cluster state: is not ok"
>                                                                          OK["AMF"] = 0
>                                                                         } else {print "AMF cluster state is ok"
>                                                  OK["AMF"] = 0
>                                                 }
>                         }
> ' sample.txt
LOTC cluster
LOTC cluster state is ok
AMF cluster
AMF cluster state: is not ok

A "reverse condition" was not specified in the original post.

Howsoever, try

awk '

/cluster state:/        {print $1, $2
                         OK[$1] = 1
                         getline
                         next
                        }

/^Node/ && OK["LOTC"]   {if ($3 != "joined")    {print "LOTC cluster state is not ok"
                                                 OK["LOTC"] = 0
                                                }
                        }
OK["AMF"]               {if (" UNLOCKED ENABLED " !~ " " toupper($NF) " ") {print "AMF cluster state: is not ok"
                                                                            OK["AMF"] = 0
                                                                           }
                        }
END                     {for (o in OK) if (OK[o]) print o, " cluster state is ok"
                        }
' file
LOTC cluster
AMF cluster
LOTC  cluster state is ok
AMF  cluster state is ok
1 Like

I tried but still same result.

awk '
>
> /cluster state:/        {print $1, $2
>                          OK[$1] = 1
>                          getline
>                          next
>                         }
>
> /^Node/ && OK["LOTC"]   {if ($3 != "joined")    {print "LOTC cluster state is not ok"
>                                                  OK["LOTC"] = 0
>                                                 }
>                         }
> OK["AMF"]               {if ("Unlocked Enabled" !~ toupper($NF))        {print "AMF cluster state: is not ok"
>                                                                          OK["AMF"] = 0
>                                                                         }
>                         }
> END                     {for (o in OK) if (OK[o]) print o, " cluster state is ok"
>                         }
> ' sample.txt
LOTC cluster
AMF cluster
AMF cluster state: is not ok
LOTC  cluster state is ok

A bit difficult to believe. What's your input data?

Here is the sample.txt input file

LOTC cluster state:
-------------------
Node safNode=SC_2_1 joined cluster | Wed Dec 5 17:03:36 2018
Node safNode=SC_2_2 joined cluster | Wed Dec 5 17:03:36 2018
Node safNode=PL_2_3 joined cluster | Wed Dec 5 17:04:34 2018

AMF cluster state:
------------------
saAmfNodeAdminState."safAmfNode=SC-1,safAmfCluster=myAmfCluster": Unlocked
saAmfNodeOperState."safAmfNode=SC-1,safAmfCluster=myAmfCluster": Enabled
saAmfNodeAdminState."safAmfNode=SC-2,safAmfCluster=myAmfCluster": locked
saAmfNodeOperState."safAmfNode=SC-1,safAmfCluster=myAmfCluster": Enabled

Looks like your result in post #7 is EXACTLY what you wanted...

1 Like

Really thankful to you guys..
If possible just explain your code..

It does the same as your code does, except once a line failed, you don't need to check further. That's what the OK array, indexed by the respective cluster state, is for. Same logic as before for the "AMF" state. If no line failed the test, OK[...] will stay 1 , and will print in the END section accordingly.

Dear RudC,

Thanks for explanation.
The Issue is now that when AMF status " Enabled and Unlocked". To me still output is AMF status is not ok.
Can you share how in your case it came as ok. Sorry to bothering you too much.
While status "Disabled and locked" it came as not ok that is correct.

$ awk '
>
> /cluster state:/        {print $1, $2
>                          OK[$1] = 1
>                          getline
>                          next
>                         }
>
> /^Node/ && OK["LOTC"]   {if ($3 != "joined")    {print "LOTC cluster state is not ok"
>                                                  OK["LOTC"] = 0
>                                                 }
>                         }
> OK["AMF"]               {if ("UNLOCKED ENABLED" !~ " " toupper($NF) " ") {print "AMF cluster state: is not ok"
>                                                                             OK["AMF"] = 0
>                                                                            }
>                         }
> END                     {for (o in OK) if (OK[o]) print o, " cluster state is ok"
>                         }
> ' sample.txt
LOTC cluster
AMF cluster
AMF cluster state: is not ok
LOTC  cluster state is ok

eshaqur@SA-00001256 ~
$ cat sample.txt
LOTC cluster state:
-------------------
Node safNode=SC_2_1 joined cluster | Wed Dec 5 17:03:36 2018
Node safNode=SC_2_2 joined cluster | Wed Dec 5 17:03:36 2018
Node safNode=PL_2_3 joined cluster | Wed Dec 5 17:04:34 2018

AMF cluster state:
------------------
saAmfNodeAdminState."safAmfNode=SC-1,safAmfCluster=myAmfCluster": Unlocked
saAmfNodeOperState."safAmfNode=SC-1,safAmfCluster=myAmfCluster": Enabled
saAmfNodeAdminState."safAmfNode=SC-2,safAmfCluster=myAmfCluster": Unlocked
saAmfNodeOperState."safAmfNode=SC-1,safAmfCluster=myAmfCluster": Enabled

You need to copy the spaces araound " UNLOCKED ENABLED " as well (c.f. post #6).

Dear RudC,

I did that change :). Dont know what small mistake i am doing. Also, tried many options..
But no luck!!!

$ awk '

/cluster state:/        {print $1, $2
                         OK[$1] = 1
                         getline
                         next
                        }

/^Node/ && OK["LOTC"]   {if ($3 != "joined")    {print "LOTC cluster state is not ok"
                                                 OK["LOTC"] = 0
                                                }
                        }
OK["AMF"]               {if (" UNLOCKED ENABLED " !~ " " toupper($NF) " ") {print "AMF cluster state: is not ok"
                                                                            OK["AMF"] = 0
                                                                           }
                        }
END                     {for (o in OK) if (OK[o]) print o, " cluster state is ok"
                        }
' sample.txt
LOTC cluster
AMF cluster
AMF cluster state: is not ok
LOTC  cluster state is ok

$ cat sample.txt
LOTC cluster state:
-------------------
Node safNode=SC_2_1 joined cluster | Wed Dec 5 17:03:36 2018
Node safNode=SC_2_2 joined cluster | Wed Dec 5 17:03:36 2018
Node safNode=PL_2_3 joined cluster | Wed Dec 5 17:04:34 2018

AMF cluster state:
------------------
saAmfNodeAdminState."safAmfNode=SC-1,safAmfCluster=myAmfCluster": Unlocked
saAmfNodeOperState."safAmfNode=SC-1,safAmfCluster=myAmfCluster": Enabled
saAmfNodeAdminState."safAmfNode=SC-2,safAmfCluster=myAmfCluster": Unlocked
saAmfNodeOperState."safAmfNode=SC-1,safAmfCluster=myAmfCluster": Enabled

Dear RudC,

Can you execute this code and share code with input file print. So, If i am doing any mistake will compare and fix.
As you get desired result in post#5.

Executing the awk script in your post #15 with exactly the data given in that post, I get

LOTC cluster
AMF cluster
LOTC  cluster state is ok
AMF  cluster state is ok

So I presume you might have a problem with your input file, like e.g. DOS line terminators <CR> ( = \r = 0x0D = ^M ) which would modify $NF beyond recognition for the comparison.

1 Like