free hit counter

Friday, August 24, 2012

awk gsub turns json into csv

I thought json would be a great sensor data collection format.  But since no one (chief among them myself) at work has spent the time to investigate and craft really good tools to manage the json data I often just end up converting it into comma separated values.

After losing this nice little one liner in my .bash_history and reconstructing it again I figured I would record it for next time.

grep accel sensor-data.json | awk 'gsub(/[,:[\]]/ , " ")' | awk '{printf("%s, %s, %s, %s\n", $2, $4, $5, $6)}' > accel-data.csv

Technically you don't need the grep at the front and could put it within its own /accel/ matching statement in one of the awk commands, but for 80MB files I figured this might help things go a little faster.