L
L
Lite_robot2015-11-09 17:37:17
linux
Lite_robot, 2015-11-09 17:37:17

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

3 answer(s)
M
Mystray, 2015-11-09
@Lite_robot

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

R
Ruslan Fedoseev, 2015-11-09
@martin74ua

[[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

why is there?

V
Victor Taran, 2015-11-11
@shambler81

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 question

Ask a Question

731 491 924 answers to any question