mardi 24 février 2015

cut for key-value pairs


I am looking for an easy way to cut key-value pairs (where the key is unique and specified) from text, much like how cut can be used to cut a specified column in a CSV file. The keys I'm looking for are not always in the same relative position in the line -- that is, using cut followed by sed won't do the trick, because they key I'm looking for is not always in the same ,-delimited column.


The text in question is indeed CSV, but it so happens that the values are key/value pairs, delimited with an =.


For example, I might parse a file with the following three lines:



Foo=1, Bar=2, Baz=3
Bar=4, Foo=2, Baz=3
Bar=42, Baz=42, Foo=3


And I would like to cut this text to yield the key/value pair for a specific key. If I was looking for Foo, then my desired output would be:



Foo=1
Foo=2
Foo=3


Ideally I would like a command-line tool that has similar syntax as cut, and can read both from stdin and from a file.


Is there such a tool?



Aucun commentaire:

Enregistrer un commentaire