parse 3rd item within a block 
Author Message
 parse 3rd item within a block

I'm trying to ferret out some info from my router configs.
(the goal is to print just the hostname and the loopback's IP address)

Let's say that I have a file that has:

!
hostname  foobar-router
!
interface Loopback0
description foobar blah blah
ip address 192.168.1.1 255.255.255.255
some other statement here
more dumb  statements here
!

I used:
awk 'BEGIN {print "The Hostname and Loopback0 address is "}
    /hostname/{print $2}
    /^interface Loopback0/,/!/{print $3}' $*

The idea is to pluck out the "foobar-router" and the IP address value
given an arbitrary number of router config files (hence the $*)

The "foobar-router" works fine (i.e. second line in my "script")

The third line says to match "interface Loopback0" to "!"
and print the third argument.  So in this example, I would get an output
that looks like:

foobar-router
192.168.1.1
statement  /* these are the third argument from lines I don't care */
statements

I need to add a && /^ip address/  so that it will only match the line
that starts with "ip address" *IN* the block that starts with "interface
Loopback0" and ends with "!"

Any idea how I can do this?  I tried tucking in the "&& /^ip address/"
statment, but awk kept barfing.

hsb

"Somehow I imagined this experience would be more rewarding"  Calvin
********************************************************************
Due to the volume of email that I receive, I may not not be able to  
reply to emails sent to my account.  Please post a followup instead.
********************************************************************



Mon, 11 Aug 2003 10:15:51 GMT  
 parse 3rd item within a block

Quote:

>I need to add a && /^ip address/  so that it will only match the line
>that starts with "ip address" *IN* the block that starts with "interface
>Loopback0" and ends with "!"

>Any idea how I can do this?  I tried tucking in the "&& /^ip address/"
>statment, but awk kept barfing.

I would do this using a flag variable:
/^interface Loopback0/{flag=1}
/!/{flag=0}
flag && /^ip address/{print $3}


Mon, 11 Aug 2003 16:53:22 GMT  
 parse 3rd item within a block

  [snip]

Quote:
> I need to add a && /^ip address/  so that it will only match the line
> that starts with "ip address" *IN* the block that starts with "interface
> Loopback0" and ends with "!"

> Any idea how I can do this?  I tried tucking in the "&& /^ip address/"
> statment, but awk kept barfing.

    # untested
    /^interface Loopback0/,/^!/ {
        if (/^ip address/) {
            print $3
        }
    }

--
Jim Monty

Tempe, Arizona USA



Tue, 12 Aug 2003 01:07:11 GMT  
 parse 3rd item within a block

Quote:

>I would do this using a flag variable:
>/^interface Loopback0/{flag=1}
>/!/{flag=0}
>flag && /^ip address/{print $3}

Thanks Bill and Jim.  I'll give your suggestion a shot and will see how
it goes.

hsb

"Somehow I imagined this experience would be more rewarding"  Calvin
********************************************************************
Due to the volume of email that I receive, I may not not be able to  
reply to emails sent to my account.  Please post a followup instead.
********************************************************************



Tue, 12 Aug 2003 09:37:17 GMT  
 parse 3rd item within a block

Quote:

>>I need to add a && /^ip address/  so that it will only match the line
>>that starts with "ip address" *IN* the block that starts with "interface
>>Loopback0" and ends with "!"
>>Any idea how I can do this?  I tried tucking in the "&& /^ip address/"
>>statment, but awk kept barfing.

>I would do this using a flag variable:
>/^interface Loopback0/{flag=1}
>/!/{flag=0}
>flag && /^ip address/{print $3}

I tried this and awk barfed.  I'm not familiar with the flag command...
can you use the "flag=" inside the pattern matching statements?

Thanks

hsb

"Somehow I imagined this experience would be more rewarding"  Calvin
********************************************************************
Due to the volume of email that I receive, I may not not be able to  
reply to emails sent to my account.  Please post a followup instead.
********************************************************************



Tue, 12 Aug 2003 13:54:05 GMT  
 parse 3rd item within a block

Quote:

> Let's say that I have a file that has:

> !
> hostname  foobar-router
> !
> interface Loopback0
> description foobar blah blah
> ip address 192.168.1.1 255.255.255.255
> some other statement here
> more dumb  statements here
> !

> I used:
> awk 'BEGIN {print "The Hostname and Loopback0 address is "}
>     /hostname/{print $2}
>     /^interface Loopback0/,/!/{print $3}' $*

> The idea is to pluck out the "foobar-router" and the IP address value
> given an arbitrary number of router config files (hence the $*)

Try

awk 'BEGIN {print "The Hostname and Loopback0 address is "}
    /hostname/{print $2}
    /^interface Loopback0/,/!/{ if (/ip address/) print $3 }' $*

--
Tapani Tarvainen



Tue, 12 Aug 2003 20:41:17 GMT  
 parse 3rd item within a block

Quote:
>> I used:
>> awk 'BEGIN {print "The Hostname and Loopback0 address is "}
>>     /hostname/{print $2}
>>     /^interface Loopback0/,/!/{print $3}' $*

>> The idea is to pluck out the "foobar-router" and the IP address value
>> given an arbitrary number of router config files (hence the $*)

>Try
>awk 'BEGIN {print "The Hostname and Loopback0 address is "}
>    /hostname/{print $2}
>    /^interface Loopback0/,/!/{ if (/ip address/) print $3 }' $*

Thanks.... but awk barfed w/ an illegal syntax message.  I'll keep
tyring.... but if anyone has any other suggestions, I'd be much obliged.

hsb

"Somehow I imagined this experience would be more rewarding"  Calvin
********************************************************************
Due to the volume of email that I receive, I may not not be able to  
reply to emails sent to my account.  Please post a followup instead.
********************************************************************



Thu, 14 Aug 2003 03:02:04 GMT  
 parse 3rd item within a block

Quote:

> >awk 'BEGIN {print "The Hostname and Loopback0 address is "}
> >    /hostname/{print $2}
> >    /^interface Loopback0/,/!/{ if (/ip address/) print $3 }' $*

> Thanks.... but awk barfed w/ an illegal syntax message.  I'll keep
> tyring.... but if anyone has any other suggestions, I'd be much obliged.

Which awk version, which operating system are you using?
That won't work in the original awk but the only place
you are likely to find it today is Solaris, and there
you can use nawk instead.

In some old versions you could try

awk 'BEGIN {print "The Hostname and Loopback0 address is "}
    /hostname/{print $2}
    /^interface Loopback0/,/!/{ if ($0 ~ /ip address/) print $3 }' $*

or

awk 'BEGIN {print "The Hostname and Loopback0 address is "}
    /hostname/{print $2}
    /^interface Loopback0/,/!/{ if ($0 ~ "ip address") print $3 }' $*

If neither of those works I'm *really* curious to know what kind
of awk you have.

--
Tapani Tarvainen



Thu, 14 Aug 2003 05:55:41 GMT  
 parse 3rd item within a block

Quote:

>Which awk version, which operating system are you using?
>That won't work in the original awk but the only place
>you are likely to find it today is Solaris, and there
>you can use nawk instead.

Ah... I'm using SunOS 5.5.1 on a Sparc LX.

Quote:
>In some old versions you could try
>awk 'BEGIN {print "The Hostname and Loopback0 address is "}
>    /hostname/{print $2}
>    /^interface Loopback0/,/!/{ if ($0 ~ /ip address/) print $3 }' $*

[snip]

This one (and using nawk with your first example) worked just fine.  But
in my sample runs (executed multiple times to rule out disk i/o), the
nawk example ran through the files in 8 seconds.  The awk example above
needed 28 seconds.  Looks like I'll be using nawk!!

Thanks again.  I'll be delving into Orielly's sed&awk book when it gets
here in a few days.

hsb

"Somehow I imagined this experience would be more rewarding"  Calvin
********************************************************************
Due to the volume of email that I receive, I may not not be able to  
reply to emails sent to my account.  Please post a followup instead.
********************************************************************



Thu, 14 Aug 2003 13:59:16 GMT  
 parse 3rd item within a block

Quote:

> Ah... I'm using SunOS 5.5.1 on a Sparc LX.

> >In some old versions you could try
> >awk 'BEGIN {print "The Hostname and Loopback0 address is "}
> >    /hostname/{print $2}
> >    /^interface Loopback0/,/!/{ if ($0 ~ /ip address/) print $3 }' $*
> [snip]

> This one (and using nawk with your first example) worked just fine.  But
> in my sample runs (executed multiple times to rule out disk i/o), the
> nawk example ran through the files in 8 seconds.  The awk example above
> needed 28 seconds.  Looks like I'll be using nawk!!

Indeed I can't think of any reason whatsoever to use old awk
for anything but old legacy scripts that don't work with nawk.
In Solaris 'nawk' is essentially what everybody else calls 'awk'
today, and /usr/xpg4/bin/awk is also equivalent with nawk.
If you want POSIX-compatibility in Solaris you might even
put /usr/xpg4/bin to the beginning of PATH (in scripts anyway).

--
Tapani Tarvainen



Fri, 15 Aug 2003 16:19:36 GMT  
 
 [ 10 post ] 

 Relevant Pages 

1. enumerate hash items within an extension ?

2. changing text properties for items within a listbox

3. Selecting an item within a menu

4. Tkinter: a widget within a menu(item)

5. Structure Sharing within parse trees

6. Structure Sharing within parse trees

7. access to variables within code blocks

8. Detect CR within a text block

9. How to insert variables within a text block

10. next iteration within a block

11. executing block at toplevel from within a class

12. Print statement within If-Then block changes output!!!????

 

 
Powered by phpBB® Forum Software