I'm trying to extract the IPv4 address of a network interface on Linux. I have a working solution:
IFACE=eth0 ip a show dev $IFACE | sed -n 's/.*inet \(.*\)\/.*/\1/p'
For the record, here is a sample output of the
ip a show command:
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000 link/ether 00:51:51:b4:a9:08 brd ff:ff:ff:ff:ff:ff inet 10.0.2.20/24 brd 10.0.2.255 scope global ens192 valid_lft forever preferred_lft forever inet6 fe80::251:56ff:fec1:4915/64 scope link valid_lft forever preferred_lft forever
While my solution works, I'm wondering if there is a sed way to grab the content of the regex pattern match group and replace the current line (in sed terms, the pattern space) with it or print it somehow?
I consider having to use
s///p and expanding the pattern to the whole line (with
.* at the beginning and the end) to be a kludge.