Intro to result filtering with Regular Expressions (regex) in Cisco IOS

Posted: 08/16/2011 by Gabe in Tools for the Network Ninja
Tags:

Is there anyone who hasn’t once gave the Show Run  or Show Tech-Support command or anything with large output and thought “man… I don’t need to see all this information ” ?  At some point you probably realized that you that you can use the pipe ->  |  <- to filter your results.   You may not appreciate the pipe until you have to look at pretty big configuration… but it’s always advantageous to learn to use it.

So if you gave this command    show run | ?  you would be presented with these useful options for filtering:

R1#show run | ?
  append       Append redirected output to URL (URLs supporting append operation only)
  begin           Begin with the line that matches the string
  exclude        Exclude lines that match the specified string
  include        Include lines that match the specified string 
  redirect       Redirect output to URL
  section         Filter a section of output
  tee                Copy output to URL

I find section to be my favorite pipe filtering choice since it includes all the associated configurations to the configuration lines matched.  They are all very helpful options and should be learned and used but they are not the purpose of my post.

The output for the show run | ? command show above would lead you to believe that adding a text string “the specified string” and pressing enter is the full extend of the filtering feature … but that is not the case.   If you issue a question mark after the filter type  show run | section ? you are presented with the following information

R2#show run | section ?
  LINE        Regular Expression
  exclude     Exclude entire section(s) of output
  include     Include entire section(s) of output

Here we find out that not only are you able to filter based on text, but you can also add a touch of coolsauce to your search with a regular expression.  Never heard of a regular expression (regex.regexp)?  Here’s a s30 minute tutorial.  You know regular expressions but don’t use them?  Grab a good cheat sheet and improve get to improving your searching kung fu.  That’s why you are here right?  You can increase the power of your search exponentially by using regex characters  . \ [ ] ^  Here’s a few basic regular expression examples:

0.0 matches 0×0 and 020

t..t matches strings such as test, text, and tart

172\.1\.. matches 172.1.10.10 but not 172.12.0.0

^123 matches 1234, but not 01234

123$ matches 0123, but not 1234

As you become proficient with regular expressions you can do advanced searches with alternation (Joe or Bob, but not Joe and Bob), Multipliers, Ranges and much more.  Sky is the limit :-]

Till next time!

~

Gabe @ networkdojo.net

 

~

Resources:

gskinner.com/RegExr

Using the Cisco IOS Command-Line Interface

Regular Expressions Cheat Sheet (v2)

The 30 minute Regex Tutorial

About these ads
Comments
  1. Ryan Ries says:

    If you use regular expressions much, you’ll love this website if you don’t already use it: http://gskinner.com/RegExr/

  2. ninjuh says:

    Awesome, I’ll add it to the post as a resource :D

    By the way – are you on Facebook ?? I need more page likes so I make a cool URL for the facebook page D:

  3. Ryan Ries says:

    Haha, no, I don’t do Facebook. I tried it out for a little while a couple years ago, and it seemed to me like it was people that weren’t actually your friends but maybe you knew them back in highschool, saying things like:

    “Waaaaaah, my day was so awful! I hate my life!”

    And then you try to be friendly and say something like,

    “Oh no, what happened?”

    And then they’re like,

    “I don’t want to talk about it.”

    -_-

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s