In Ubuntu 16.04 I ran ifconfig
and saw my external ip as in inet addr:MY_IP
.
I tried to "dug" it right into a variable in these ways:
ipa=$(ifconfig | grep "inet addr:\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b")
and:
ipa=$(ifconfig | grep "inet addr:\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.")
These methods work:
ipa=$(ifconfig | grep -Po 'inet addr:\K[^\s]+' | grep -v '^127')
and
ipa=$(ifconfig | grep -A 1 eth0 | grep -Po "inet addr:(\d{1,3}\.){3}\d{1,3}" | cut -f2 -d:)
But I would like to know, please, what I've missed in my first 2 tryings.
Update:
Is there a way to use one grep
with 4 groups (similar to the concept of the first 2) that will indeed work in POSIX BRE?