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.