Here's one way using grep
:
line='inet 192.168.2.13 netmask 0xffffff00 broadcast 192.168.2.256'
echo "$line" | grep -oE "([0-9]{1,3}.){3}[0-9]{1,3}"
Results:
192.168.2.13
192.168.2.256
If you wish to select only valid addresses, you can use:
line='inet 192.168.0.255 netmask 0xffffff00 broadcast 192.168.2.256'
echo "$line" | grep -oE "((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)"
Results:
192.168.0.255
Otherwise, just select the fields you want using awk
, for example:
line='inet 192.168.0.255 netmask 0xffffff00 broadcast 192.168.2.256'
echo "$line" | awk -v OFS="
" '{ print $2, $NF }'
Results:
192.168.0.255
192.168.2.256
Addendum:
Word boundaries:
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…