Re: exception using AWK


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Re: exception using AWK
# 1  
Old 09-12-2012
Re: exception using AWK

I have following file:

Code:

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:

Code:
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

Last edited by rcc50886; 09-13-2012 at 12:23 AM..
# 2  
Old 09-12-2012
This should print the previous name value when target is 'offline' and state is 'online':

Code:
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.
# 3  
Old 09-13-2012
I want to print

when STATE != TARGET

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

Code:
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.
# 4  
Old 09-13-2012
Then this:

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

This User Gave Thanks to agama For This Post:
# 5  
Old 09-13-2012
Thanks allot !! It worked for me.

Thanks again for your help.

Last edited by rcc50886; 09-13-2012 at 12:52 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Parsing output with awk - need assistance on exception

HI Folks - I have a business need to check weather or not there are "active" sessions within a particular application I interact with prior to running any automation. I have built a function that first exports all "sessions" from my respective application to a text file. The output is as... (2 Replies)
Discussion started by: SIMMS7400
2 Replies

2. Shell Programming and Scripting

Monitor logs for exception and if exception come then sent an email

Hi Folks, please advise , I have logs generated on unix machine at location /ops/opt/aaa/bvg.log , now sometimes there come exception in these logs also, so I want to write such a script such that it should continuously monitor these logs and whenever any exception comes that is it try to find... (3 Replies)
Discussion started by: tuntun27272727
3 Replies

3. Solaris

solaris 7 exception

Hi all, An application works well under 2.6 but under 7 it gives TEXT_IO exceptions. (Is_Open, Check_Is_Open, Get_Line procedures). Any idea? Regards (3 Replies)
Discussion started by: endoavour
3 Replies

4. Programming

Exception Handling C++

Hello All, I have a question ....which I am totally confused about! If I have a fxn foo in a program which returns a logical value. But it has a posssiblity to throw some exception. Now my exception handler returns a value as a string stating why the exception occured. But my... (1 Reply)
Discussion started by: mind@work
1 Replies

5. Shell Programming and Scripting

Exception handling

Sometimes when I try to use curl to upload to an ftp server, I get the message: $curl -T file.wmv ftp.eu.filesonic.com --user user:password curl: (8) Got a 421 ftp-server response when 220 was expected How do I get the script to try again if I get the message curl: (8)? (2 Replies)
Discussion started by: locoroco
2 Replies

6. Shell Programming and Scripting

Exception Handling

Hi, I have written a script to load csv files into a mysql database, however, i would like for the shell script to exit in the event of an error (missing file, load error etc.) - currently if an error is encountered the next statement is processed - This is how i am loading the csv scripts ... (5 Replies)
Discussion started by: bertpereira
5 Replies

7. Shell Programming and Scripting

Perl Exception - $!,$?,$@

Hi, I am trying to understand the significance of the special variables $!,$@ and $? in perl. I have a code block as follows: eval { Code Segment 1: #authenticating to the remote server $ftpobj -> login($username,$password) or die "Can't login to $remote_host"; ... (12 Replies)
Discussion started by: DILEEP410
12 Replies

8. Linux

MMU exception

I hope to post in the right forum, otherwise I apologize for this. if a MMU exception is caused by a process which tries to access to other memory segment (out of its own address space) what the kernel does in this case ? maybe kernel kills the "bad" process ? (2 Replies)
Discussion started by: Puntino
2 Replies

9. UNIX for Dummies Questions & Answers

How to catch the exception

Dear friends, I am transferring some files to a windows system from Unix m/c thru FTP Script given below. echo "open $host quote USER $userid quote PASS $pwd $verbose $type cd $dir bin put $file close quit"|$ftp... (0 Replies)
Discussion started by: Vijayakumarpc
0 Replies

10. UNIX for Advanced & Expert Users

exception handling

Does exception handling exist in any UNIX enviornment? I develop on Windows MSVC++ land and need to port to UNIX. (1 Reply)
Discussion started by: RichardS
1 Replies
Login or Register to Ask a Question