There's an output like this:
RA#show segment-routing traffic-eng on-demand color detail | utility egrep Color -B 10
Sat Dec 25 11:24:22.891 JST
SR-TE On-Demand-Color database
------------------------
On-Demand Color: 20
--
Performance-measurement:
Reverse-path Label: Not Configured
Delay-measurement: Disabled
Liveness-detection: Enabled 《-------
Profile: liveness1
Invalidation Action: down
Logging:
Session State Change: Yes
Per-flow Information:
Default Forward Class: 0
On-Demand Color: 23
--
Performance-measurement:
Reverse-path Label: Not Configured
Delay-measurement: Disabled
Liveness-detection: Enabled 《--------
Profile: liveness1
Invalidation Action: down
Logging:
Session State Change: Yes
Per-flow Information:
Default Forward Class: 0
On-Demand Color: 301
regex "On-Demand Color:\s(\S )" can be used to extract color "20,23,301", but as Liveness-detection was not enabled under "On-Demand Color: 301", So I expected only color "20" and "23" can be extacted. Is it possible to achieve this by regular expression?
CodePudding user response:
Yes, it is possible to do this with regex, although beware that a parser would probably be better in this scenario. The idea is to search for the string that you want as long as it has a "Liveness-detection: Enabled" afterwards.
The regex would look like this:
On-Demand Color:\s(\S )\n.*\n.*\n.*\n.*\n.*Liveness-detection: Enabled
Demo: https://regex101.com/r/SElvt2/1
And basically it matches also a few lines after you find the "On-Demand Color:" string. This regex can further be simplified to this:
On-Demand Color:\s(\S )(\n.*?)*Liveness-detection: Enabled
Demo: https://regex101.com/r/857JQM/1
So that the "Liveness-detection:" string can be a few lines before of after the expected position.
CodePudding user response:
For your 2nd method, this might be better.
On-Demand Color:\s(\S )(?:\n.*){5}Liveness-detection: Enabled
CodePudding user response:
If you want to be a bit more flexible and if the value can be present after n number of lines instead of exactly 5, you can make use of a negative lookahead.
This will also prevent overmatching if Liveness-detection: Enabled is not present in the current On-Demand Color: part.
On-Demand Color:\s(\S )(?:\n(?!On-Demand Color:|\s*Liveness-detection:).*)*\n\s*Liveness-detection: Enabled
The pattern matches:
On-Demand Color:
Match the starting string\s(\S )
Match a whitespace char and capture 1 non whitespace chars (or use(\d )
for digits) in capture group 1(?:
Non capture group\n(?!On-Demand Color:|\s*Liveness-detection:)
.*
Match the whole line
)*
Close non capture group and optionally repeat\n\s*Liveness-detection: Enabled
Match a newline, optional whitespace chars and the string that you want to match