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/ether2: 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 foreverFor 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 mtumtu 1500inet 10.0.2.15/24brd 10.0.2.255
Matching keyword value pairs from semi-structured input
shell script;text processing
Assuming that key name will appear at most once, here is how you may try. ip addr show dev eth0 | perl -ne 'chomp; foreach $param (qw(mtu inet brd)) { /($param)\s+(\S+)/ && do { print $1 $2\n; }}'