jeudi 26 mars 2015

CSV Units Conversion


I've got a CSV file that looks like the following:



miami,20,in
lansing,2,cm
austin,3,mm
chicago,5,miles
phoenix,2,feet


The first field is the name, the next field is the numeric value and the third field indicates the units. I've listed all the units that my dataset currently has however it is not limited to these as I expect the dataset to change and require different units of measurement over time.


I want to be able to process this CSV file in a script so that all my values are converted to one common unit, inches, and are listed alongside the name. So it should look something like this:



miami,20
lansing,0.78740157
austin,0.11811024
chicago,316800
phoenix,24


I've figured out that there's a unit conversion tool units which is precisely what I'm looking for to use to convert my units as my data contains a mixture of metric and imperial units.


For example, if I wanted to convert my last line of data into inches I would do this



units 2cm in -t


Which would give me



0.78740157


What I'm currently seeking help on is how to go about using this command in a script and outputting it in the format I described (2nd code block). The CSV data is currently stored in a variable called $citydata in a bash script. It is currently a string.


​​​



Aucun commentaire:

Enregistrer un commentaire