In terms of regex, the PCRE-compatible expression (?:[12]?\d{1,2}\.){3}[12]?\d{1,2}
should meet your needs. It's a simplified version of the more comprehensive IP regexes that can be found as answers on this question, and can be tested with this demo.
Unfortunately, awk
is quite limited in its ability, and is not PCRE compatible. I would suggest using perl instead, but if you're insistent on using awk, the following command should work:
awk 'match($0, /[12]?[0-9]?[0-9]\.[12]?[0-9]?[0-9]\.[12]?[0-9]?[0-9]\.[12]?[0-9]?[0-9]/) {print substr($0, RSTART, RLENGTH)}'
This uses awk
-compatible regex to match IPs, and is an expanded form of the above regex. It matches and prints out only the IPs it finds, omitting the rest of the line.
Before you edited your question, your original regex was 0-9]+.[0-9]+.[0-9]+.[0-9]+
- the .
allowed it to match any character, meaning hyphens, spaces, and numbers were all valid matches. By specifying \.
instead, the regex will exactly match the period character.