cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Announcements

343
Views
0
Helpful
7
Replies
Highlighted
Beginner

IOS shell regular expressions

I want to grep through the output of "show vlan brief" on several kinds of Catalyst switches (mostly 2960s but also a 6880X) running 15.2(1)S. If I save the output to a Linux box I can "grep [0-9]$" and get the same output that "show vlan brief | include [0-9]$" produces. But the IOS version of grep doesn't do anything with the [0-9]$ syntax. Is the regex parser just broken or incomplete or is there a secret sauce hidden away in a file I haven't Googled for correctly yet?

1 ACCEPTED SOLUTION

Accepted Solutions
VIP Advisor

Re: IOS shell regular expressions

Hi there,

The best guide you will find is here, bear witness to how horribly limited it is:

https://www.cisco.com/c/en/us/td/docs/ios-xml/ios/fundamentals/configuration/15_sy/fundamentals-15-sy-book/cf-cli-search.pdf

 

The regex secret sauce is actually hidden in the EEM sub-system:

https://www.cisco.com/c/en/us/td/docs/ios-xml/ios/eem/command/eem-cr-book/eem-cr-a1.html#wp1168104291

...fully featured implementation. This is great for working on output generated within your EEM script, but you then need a strategy to extract the results.

It is a mystery why the IOS CLI never used the EEM regex engine. If you want a switching platform with superb CLI regex, take a look an junos.

 

Cheers,

Seb.

7 REPLIES 7
VIP Advisor

Re: IOS shell regular expressions

here is the regular expression how it works 

 

https://www.cisco.com/c/en/us/td/docs/ios-xml/ios/fundamentals/configuration/15_sy/fundamentals-15-sy-book/cf-cli-search.pdf

depends on what IOS, but 15.0X should work.

 

your command works. on my switch just tested(Version 15.2)

 

SW1# show vlan brief | include [0-9]$
1 default active Et1/3, Et2/3, Et3/0, Et3/1
Et3/2
9 VLAN0009 active Et3/3
10 VLAN0010 active Et2/0
30 VLAN0030 active Et2/2
100 VLAN0100 active Et2/1

 

BB
*** Rate All Helpful Responses ***
Beginner

Re: IOS shell regular expressions

Yes, you are repeating what I said, which is that the regular expression works with CLI commands, but not with grep in the shell. And, the pipe to include doesn't work in the shell, either, so you can't start a command with CLI syntax and pipe it to a shell command.

 

My output:

 

Switch-5th-Floor-Lab#sh ver
Cisco IOS Software, C2960C Software (C2960c405-UNIVERSALK9-M), Version 15.2(2)E, RELEASE SOFTWARE (fc3)

************************************************
     Output deleted
************************************************


Configuration register is 0xF

Switch-5th-Floor-Lab#sh vlan brie | i [0-9]$
1    default                          active    Gi0/2
203  Switch_Floor_5                      active    Fa0/1, Fa0/2, Fa0/3, Fa0/4
                                                Fa0/5, Fa0/6, Fa0/7, Fa0/8
605  Switch-5thFloor-VOIP                active    Fa0/1, Fa0/2, Fa0/3, Fa0/4
                                                Fa0/5, Fa0/6, Fa0/7, Fa0/8
Switch-5th-Floor-Lab#term shell
Switch-5th-Floor-Lab#sh vlan brie | grep [0-9]$
1    default                          active    Gi0/2
2    XXXXXXX network                  active

************************************************
     Output deleted
************************************************

200  XXX-EastSection                  active

************************************************
     Output deleted
************************************************

1002 fddi-default                     act/unsup
1003 trcrf-default                    act/unsup
1004 fddinet-default                  act/unsup
1005 trbrf-default                    act/unsup
Switch-5th-Floor-Lab#

 

The entire list of VLANs is returned.

 

Cisco's docs says regular expressions work with their grep, but as far as I can tell, it's just very simple stuff like 2$, which isn't very helpful. I was looking for information from anyone who can tell me if my command line needs tweeking, or if the regular expression parser in the shell in this version of code is just not ready for prime time. It would be nice if you could string commands together with pipes like you can in a real shell, but if the first one chokes then the shell is pretty useless.

VIP Advisor

Re: IOS shell regular expressions

Oh correct, i was in different context while looking. its is like Linux BSD Grep cisco used.

 

Still i am not clear what you expect on Compacted prebuild grep in cisco IOS.

 

This is not like Full shell stack how you see in Linux.

 

Can you give example what you look, i may be reading wrong.

 

BB
*** Rate All Helpful Responses ***
Beginner

Re: IOS shell regular expressions

I want to pipe the output to another text utility, like cut (which also does not seem to work correctly, but one question at a time), and then another, and eventually end up with a list of the VLANs without all of the other text the CLI output produces. I can easily come up with any number of command strings to do this on a real Linux box, but I need to save the original output as a file first. It would be nice to avoid that time-consuming step, and I assume that is what cisco intended by creating the terminal shell in the first place. I have tried all kinds of variations on the [0-9]$ syntax, escaping the brackets, using single and double quotes, etc., but as far as I can tell these text utilities are so limited that finding out what they can do takes longer than just doing what I need to do another way.

VIP Advisor

Re: IOS shell regular expressions

As i have mentioned earlier post this got limited set of commands, you want to full linux based command you can use linux box get out from device and can trim how you like.

 

you can use expect command.

BB
*** Rate All Helpful Responses ***
VIP Advisor

Re: IOS shell regular expressions

Hi there,

The best guide you will find is here, bear witness to how horribly limited it is:

https://www.cisco.com/c/en/us/td/docs/ios-xml/ios/fundamentals/configuration/15_sy/fundamentals-15-sy-book/cf-cli-search.pdf

 

The regex secret sauce is actually hidden in the EEM sub-system:

https://www.cisco.com/c/en/us/td/docs/ios-xml/ios/eem/command/eem-cr-book/eem-cr-a1.html#wp1168104291

...fully featured implementation. This is great for working on output generated within your EEM script, but you then need a strategy to extract the results.

It is a mystery why the IOS CLI never used the EEM regex engine. If you want a switching platform with superb CLI regex, take a look an junos.

 

Cheers,

Seb.

Beginner

Re: IOS shell regular expressions

Thank you, Seb. I've done a lot of work in Junos environments, and you're right. My current workplace is Cisco-only, so I was hoping the shell would be useful. Anyway, I didn't know about the EEM, so that will be interesting to look at. Thanks again.

CreatePlease to create content
Content for Community-Ad
July's Community Spotlight Awards