cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
8856
Views
6
Helpful
8
Replies
Highlighted
Beginner

pipe command usage

I would like to skim through my show run command, and grep out only the "interface" and whether "bpduguard enabled" on that interface. Something like:

sh run | incl (interface | bpduguard)

thanks

jimmyc

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted

Hi,

It works because the second pipe is taken as an alternation character (i.e. "OR") so what happens is that for each line in the config the parser searches for either "interface" or "bpduguard" and prints out a match if it finds either one.

See the chapter "Using the IOS CLI" in the configuration fundamentals book:

http://www.cisco.com/en/US/products/ps6350/products_configuration_guide_chapter09186a008044036c.html#wp1002025

It also works for multiple pipes, e.g. "show run | include interface | duplex | speed"

HTH

Andrew.

View solution in original post

8 REPLIES 8
Highlighted
Hall of Fame Cisco Employee

If your device is running 12.3(2)T or higher (or 12.2(18)SXF5 or 12.2(40)SE or higher), you can use tclsh to do this. For example:

proc find_matching_ints { cmd } {

set output [exec "show run"]

set ints [list]

set currint ""

foreach line [split $output "\n"] {

if { [regexp {^interface (.*)$} $line ignore interface] } {

set currint [string trim $interface]

}

if { [regexp "^\\s+$cmd\[\\r\\n\]?$" $line] } {

lappend ints $currint

}

}

foreach int [lsort -unique $ints] {

puts $int

}

}

find_matching_ints "bpduguard enable"

Highlighted

Hi Joe

This question has come up quite a few times. Do you know if Cisco have any plans to extend their regex syntax to include things such as matching across lines etc. It would be a very useful function if they did.

Alternatively it would be almost as useful if you could use multiple pipes eg

sh run | begin interface | include bpduguard

Jon

Highlighted

Joe

Actually that pipe example wouldn't work but still multiple pipes would be useful for other things.

Jon

Highlighted
Hall of Fame Cisco Employee

I haven't heard of any plans to add multiple pipe support or context matching support. The thought may be that TCL is coming to all platforms eventually, and it will offer a broader flexibility.

Highlighted

I tried something that worked, I don't know how or why. Here is the exact syntax:

sh run | incl interface | bpduguard

I would have expected that I would be required to use parentheses, like my earlier example.

Version = Cat4507 12.2(25)EWA8

Highlighted

Hi,

It works because the second pipe is taken as an alternation character (i.e. "OR") so what happens is that for each line in the config the parser searches for either "interface" or "bpduguard" and prints out a match if it finds either one.

See the chapter "Using the IOS CLI" in the configuration fundamentals book:

http://www.cisco.com/en/US/products/ps6350/products_configuration_guide_chapter09186a008044036c.html#wp1002025

It also works for multiple pipes, e.g. "show run | include interface | duplex | speed"

HTH

Andrew.

View solution in original post

Highlighted

Hi Andrew,

I could not find that paragraph that explains where the "|" is equal to "or". Can you point me specifically to that text.

thanks.

Highlighted

Never mind, it was right there under alternation character, just like you said. Why can't Cisco use English, and say "or" instead of alternation character?!!!

Thanks for your help.

Jimmyc

Content for Community-Ad