Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I would like to know if have some sed, grep or awk regex to parse Cisco interface section and exclude if interface is down (shutdown), like example below.

Example:

!
interface FastEthernet0/1
 ip address 10.16.0.1 255.255.255.224
!
interface FastEthernet0/2
 ip address 10.17.0.1 255.255.255.224
 shutdown
!

I'm using sed regex below to parse interface section:

sed -n -e '/^interface/,/^!/p' file.txt

But the regex above, parse all interface and not exclude interface down.

share|improve this question
    
FYI, you asked for shell tools; however, ciscoconfparse is a python module which specializes in handling these kind of tasks. If you do a lot of this, it may be worth your while to consider using a library which is built for parsing cisco configs. –  Mike Pennington Feb 8 at 19:29

1 Answer 1

awk

With GNU awk or mawk you could do it like this:

awk -v RS='!\n' '!/shutdown/'

Output:

interface FastEthernet0/1
 ip address 10.16.0.1 255.255.255.224

You could make the match more precise if you only look for shutdown in the third line:

awk -v RS='!\n' -v FS='\n +' '$3 !~ /shutdown/'

sed

You can of course also use sed, but it is more complicated.

One approach is the one you've started with, but collect the whole record in hold space before printing anything. This works with GNU sed:

sed -n '/^interface/,/^!/ H; 1b; /^!/ { z; x; s/^\n//g; /shutdown/!p }'
share|improve this answer
    
Thank you very much. It's work. –  user32634 Feb 20 '13 at 11:55
1  
@user32634: I'm glad it helped you. The way to say thanks around here is by up-voting useful answers and accepting the most useful answer. –  Thor Feb 20 '13 at 12:55

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.