It's possible, but not elegant:
echo 'This sentence contains an ip number 1.2.3.4 and port number 50, i want to print the IP address only.' \
| sed 's/.*\([0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\).*/\1/'
[0-9]
matches any digit, \{1,3\}
means it can be repeated 1 to 3 times. \.
matches a dot. The whole IP is captured by the \(...\)
parentheses, what comes before and after is matched by .*
, i.e. anything repeated zero or more times. The whole matching string (i.e. the whole line) is then replaced by the contents of the first matching group.
You can make it more readable by introducing a variable:
n='[0-9]\{1,3\}'
... | sed "s/.*\($n\.$n\.$n\.$n\).*/\1/"
It prints the whole string if the IP is not found. It also doesn't check for invalid IPs like 256.512.999.666.