I read a line of data in this form:
KEYNAME:value,KEYNAME:value,KEYNAME:value
(please note keys are strings without spaces and values are integers)
I need to select some specific keys (without knowing their relative positions) and print their values on separate lines:
value
value
value
So far I came to something like converting the input line to multiple KEY:value lines, grepping the keys I want, imposing a known order and finally passing that through cut again to remove the key part:
sed 's/,/\n/g' | egrep '(FooKey|BarKey|BazKey)' | sort | cut -d':' -f2
I thought I could arrange something with awk, of which I just know the basics, but I can only manage fields by their positional order and not by their name (key name).
Could be done with other languages (i.e. Python or Perl) but I’m taking it as an exercise to do everything with the usual shell tools. Besides, I would still prefer the above solutions to loading up a bigger interpreter like python or perl.
This will print only the values:
If you want the values in a specific order, you can put them in an array: