Answer the question
In order to leave comments, you need to log in
Grep: how to fix regex with lookahead?
There is such a regular expression: " [0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3 }(?=\/[0-9]{1,2}) " to get the ip address followed by the subnet prefix.
When using " ip route show | grep -E -o "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\. [0-9]{1,3}(?=\/[0-9]{1,2})" " for some reason, nothing is found, although on the site for testing regular expressions, this regular expression works.
If you remove (?=\/[0-9]{1,2}) then the regular expression starts to work.
How to make regular expression " [0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1, 3}(?=\/[0-9]{1,2}) " ?
Answer the question
In order to leave comments, you need to log in
What you want is not ERE (grep -E) mode, but PCRE (grep -P).
In normal (even extended) mode, grep doesn't know how to lookarounds and much more
www.regular-expressions.info/refadv.html
[[email protected] ~]$ ip addr show dev lo | grep -E -o "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}(\/[0-9]{1,2})"
127.0.0.1/8
do you need something like this?
ifconfig | grep inet | grep -v inet6 | grep -v 127.0.0.1 | cut -d: -f2 | awk '{printf $1"\n"}'
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question