Cisco IOS CLI Regex: sh ip bgp in

Vitaly left a comment on a blog post with an IOS CLI Regex tip. Here it it:

router#sh ip bgp | in (.*)( +)(.*)( +)0( +)100
*>i10.10.11.0/24 0 100 0 i

I want pick this apart because I have almost no regex skills and there is only one way to learn them – practice and repetition.

This bit should be fairly obvious. Show routes from the BGP Routing table.
router#sh ip bgp | in

Let’s break down this piece

(.*)( +)(.*)( +)0( +)100

* round brackets () are used for grouping action.
* . matches any single character except a newline.
* ( +) will match any number of spaces. The + is a greedy quantifier for e.g. (ab)+ matches all of ababababab
* the 0 is a hard match and must match only ‘0’
* then any numbers of spaces again.
* match one hundred.

The 0 is the weight of the BGP Route and the 100 is the local preference. Therefore the command is showing all BGP routes that weight of zero and Local Pref of 100.

However, I think that I could achieve the same thing with
router#sh ip bgp | in (.*)0( +)100
since this would match the any characters, then the pattern 0, any spaces, 100. Or even shorter

router#sh ip bgp | in 0( +)100

Shorter still (because the plus is a greedy operand)
router#sh ip bgp | in 0 +100
but this doesn’t work quite right. If your weight ends in a zero such as 10, or 100 it will still match.

The EtherealMind View

Am I missing something ? I’m not sure what the purpose of this command would be other than to match on weight and localpref. It possible that there is some hidden effect in (.*)( +)(.*)( +) that I can’t see. If so, I’d like to know.

Anyone got anymore special commands that I could take a look at. Leave some notes in the comments.

Link to original comment – Thanks Vitaly

Other Posts in A Series On The Same Topic

  1. Cisco IOS CLI Regex: sh ip bgp in (2nd May 2012)
  2. IOS CLI Tip: More accurate pipe commands (1st May 2012)
  3. Cisco Nexus NXOS and Fixing broken “switchto” syntax with alias (18th December 2011)
  4. show ip eigrp topology all (22nd May 2011)
  5. Cisco IOS CLI Shortcuts (6th February 2011)
  6. The poor man's IOS Traffic Generator (19th September 2009)
  7. IOS: "terminal monitor" on, off - logging to your terminal (17th September 2009)
  8. IOS: Console, Terminal, Monitor, VTY - what is what ? (16th September 2009)
  9. IOS: Clearing an interface configuration (13th September 2009)
  10. IOS: Setting Terminal Window Length (10th September 2009)
  11. IOS CLI: show run linenum (9th September 2009)
  12. IOS: Setting the TCP timeout on IOS (14th August 2008)
  13. IOS: enable and .... disable ? (20th July 2008)
  14. IOS: Reverse SSH console access - Part 2 (25th June 2008)
  15. IOS:Open Source Lab DNS and IP addressing (2nd June 2008)
  16. IOS: Reverse SSH console access (29th May 2008)
  17. ip tcp timestamp (14th April 2008)
  18. Cisco ASA and IOS command tip - test aaa-server (18th February 2008)
  • Derick Winkworth

    “show ip bgp | include _ 0 +100”

    Thats underscore, space, 0, space, 100

    That should accomplish the same thing.  I can only think he wants to limit the output of the show ip bgp command to lines with prefixes in them.  Or perhaps lines with BGP routes from other routers?  Weight is only local to a given router.  0 means the route was learned from another router (barring any policies that modify weight of learned routes).

  • Dave Noonan

    Cisco regex might be different but in most cases regex is greedy by default and will match the largest possible chunk of text.  The + actually means match “1 or more” of the preceding element  while * means match “0 or more”.  So “0 *100” would match 0100 while “0 +100” would not. They would both match “0 100” with any number of spaces between the 0 and 100.

  • Vitaliy Soldatov

    It was just regex example. Derick ”
    I can only think he wants to limit the output of the show ip bgp command to lines with prefixes in them. ” This is correct.

    Here’s another:

    cme#sh run | beg dn(.*)27
    ephone-dn 27 dual-line
    number 2027
    description A.V. Ivanov
    name A.V. Ivanov
    huntstop channel

    cme#sh run | beg dial(.*)9
    dial-peer voice 9 pots
    destination-pattern 9T
    progress_ind alert enable 8
    port 1/0:15
    forward-digits all

    router#sh run | beg ^router isis
    router isis
    net 49.0000.0024.0000.0001.7206.00
    is-type level-2-only
    metric-style wide

    And some script for configuring bgp and prefix-lists

    #!/usr/bin/env python

    import re
    from netaddr import *
    except IOError:
       print “route.txt not found”

    for line in fh.readlines():
          print “network %s mask %s”%(,ip.netmask) 
          print “ip prefix-list STATIC_to_BGP permit %s”%(pref)

  • James Harr

    Grouping really isn’t necessary, but might be desirable from a readability point of view:

    sh ip bgp | in .* +.* +0 +100 

    At this point, the leading .* is required to convince IOS that the space (in ” +”) is part of the argument and not just something that gets stripped out.

    Worth noting that in NX-OS, you need quotes around a regex that contains a space:
    sh ip bgp | in “.* +.* +0 +100” 
    It’s a very unixy way of lexing arguments, which I like. The inconsistency between IOS and NX-OS, not so much.

    In a lot of programming languages, a group has two functions:
    1. To repeat a series of atoms (atoms being single characters) with +, *, ?, or in more sophisticated regex implementations, {1,5} (match 1-5 times)
    2. To extract the matching text.

    Caveat: Regex support probably varies widely throughout IOS/NX-OS. Unix isn’t really consistent in its regex support (what you need to escape, what shortcuts are there, etc). I have the strange ability to remember that sort of arcane stuff, but I hear OReilly’s Regex Pocket Reference is great to keep around if you want to practice the more powerful stuff.

  • Krunal Shah

    how about
    show ip bgp regexp
    gw03#sh ip bgp ?

      regexp             Display routes matching the AS path regular expression