On occasion I get output from a command that includes "key value" pairs, with possibly more than one pair per line. As a repeatable example, consider the command ip addr show dev eth0:
ip addr show dev eth0 | grep -v link/ether
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
inet 10.0.2.15/24 brd 10.0.2.255 scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::a00:27ff:fe0f:dbb3/64 scope link
valid_lft forever preferred_lft forever
For the purposes of this example I might want to capture the values following mtu, inet, and brd (i.e. 1500, 10.0.2.15/24, and 10.0.2.255 respectively, here). It can be assumed that each keyword will not occur more than once in the input.
The only way I can see to handle this would be capture the output of the source command to a temporary variable, and repeatedly parse that until all the keywords have been processed.
Is there an easier or better way to pick out these "key value" pairs, discarding the remainder of the text?
Suggested example provision and unordered output:
ip addr show dev eth0 | someCommand brd inet mtu
mtu 1500
inet 10.0.2.15/24
brd 10.0.2.255
... | tr \ \\n | sed -E '/^(mtu|inet|brd)$/!d;N;s/\n/ /'; this assumes your keys and values are separated by a single space and keys/values cannot contain spaces.sedfor simple search/replace and avoid its loop and buffer options). Your example has given me opportunity to try outNtoo. Thank you!