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