Re: exception using AWK

I have following file:



NAME=ora.DG1.svc
TYPE=ora.service.type
CARDINALITY_ID=1
TARGET=ONLINE
STATE=ONLINE

NAME=ora.orlene.DG2.svc
TYPE=ora.service.type
CARDINALITY_ID=1
TARGET=ONLINE
STATE=OFFLINE

NAME=ora.MN.acfs
TYPE=ora.registry.acfs.type
TARGET=ONLINE
STATE=ONLINE

NAME=ora.scan1.vip
TYPE=ora.scan_vip.type
CARDINALITY_ID=1
TARGET=ONLINE
STATE=ONLINE

I need to print the NAME filed only if the TARGET and STATE are equal. I can get that from following:

awk -F"=" '/NAME/{name=$2;} {if($0~/TARGET/){tar=$2;} else if($0~/STATE/){st=$2;if(st!=tar)print name;}}' file.txt

But I need to exclude NAME if the ( TARGET=OFFLINE and STATE=ONLINE )

How can i do that using awk.

-Thanks

This should print the previous name value when target is 'offline' and state is 'online':

awk -F = '
    /^NAME/ { n = $2; next }
    /^TARGET/ { t = $2; next; }
    /^STATE/ {
        if( t == "OFFLINE" && $2 == "ONLINE" )
            print n;
    }
' file.txt

It makes the assumptions that NAME, STATE, and TARGET are always the first tokens on each record with no leading whitespace and that there isn't more than one name=value pair per line.

I want to print

when STATE != TARGET

but with one exeception i.e ( TARGET=OFFLINE and STATE=ONLINE)

It makes the assumptions that NAME, STATE, and TARGET are always the first tokens on each record with no leading whitespace and that there isn't more than one name=value pair per line.

Your assumption is true.

---------- Post updated at 10:27 PM ---------- Previous update was at 10:12 PM ----------

Can anyone help-me out ? I am basically sql developer but learning the UNIX now.

Then this:

awk -F = '
    /^NAME/ { n = $2; next }
    /^TARGET/ { t = $2; next; }
    /^STATE/ {
        if( t != $2 && !(t == "OFFLINE" && $2 == "ONLINE") )
            print n;
    }
' input-file
1 Like

Thanks allot !! It worked for me.

Thanks again for your help.