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 the series
- Cisco IOS CLI Regex: sh ip bgp in
- IOS CLI Tip: More accurate pipe commands
- Cisco Nexus NXOS and Fixing broken “switchto” syntax with alias
- show ip eigrp topology all
- Cisco IOS CLI Shortcuts
- The poor man's IOS Traffic Generator
- IOS: "terminal monitor" on, off - logging to your terminal
- IOS: Console, Terminal, Monitor, VTY - what is what ?
- IOS: Clearing an interface configuration
- IOS: Setting Terminal Window Length
- IOS CLI: show run linenum
- IOS: Setting the TCP timeout on IOS
- IOS: enable and .... disable ?
- IOS: Reverse SSH console access - Part 2
- IOS:Open Source Lab DNS and IP addressing
- IOS: Reverse SSH console access
- ip tcp timestamp
- Cisco ASA and IOS command tip - test aaa-server