How can I make regex to search for ipv4 address only. when I am doing
grep -E '([0-9]\.){1,3}[0-9]\b filename
It is showing group with five octets also.
How can I make regex to search for ipv4 address only. when I am doing
grep -E '([0-9]\.){1,3}[0-9]\b filename
It is showing group with five octets also.
I doubt it can be done with a POSIX regex, thus I'd rather suggest a PCRE solution:
grep -P '(?<!\d\.|\d)\d{1,3}(?:\.\d{1,3}){3}(?!\.?\d)' filename
The pattern matches
(?<!\d\.|\d)
- a location that is not immediately preceded with a digit and a dot or just a digit\d{1,3}
- 1 to 3 digits(?:\.\d{1,3}){3}
- three occurrences of
\.
- a dot\d{1,3}
- 1 to 3 digits(?!\.?\d)
- a location that is not immediately followed with an optional dot and then a digit.To make the pattern a bit more precises, replace the octet pattern (\d{1,3}
) with (25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)
.