Key/Value Pair in BASH!?

A few nights ago, I was watching some television, drinking a beer, and skimming some articles on managing one’s unix config files with Git. Most of the articles phone it in. They would get to creating a directory with your config files and symlinking them.

Creating those manually!? Ain’t nobody got time for that!

My beer buzzing brain figured I should roll up my sleeves and write a little something to manage that. While thinking about how this would look, I screwed up my face at the giant Case statement growing in my mind. Gotta be a better way…

Key/Value pairs. Perfect for this script. A google search hit on this stackoverflow article very succinctly explaining them. Have a quick read. Seriously, it’s quick.

Welcome back! There’s a ton more info on it in the BASH man page. If you search with

Associative arrays are created using declare -A

you’ll find the good part.

Getting back to my story, with my trusty “key/value pair” A.K.A. “associative array” I knocked together a little script to setup symlinking in my Git managed “config” directory. You can check it out here, if you are so inclined. It was suggested, getting a config file with the key/value pairs would be bad ass. Maybe I’ll get to it. Right now, I should get to a beer.

Sleep data graphs

I’ve been using the Sleep as Android app to track my sleep for a little while now.  The application allows exporting to CSV files. I thought, “GREAT! I can bash in some, well, BASH and put this in Graphite“. You know to look at it in relation to other things I’m tracking and wastes of time like that.

I cracked open the CSV and immediately noticed it was REALLY whacky. Okay, well, not whacky, just not what I expected. I was expecting a row of data descriptions with a day’s values per row. This was going to take more work than I anticipated.

I took a spin around the Googles to see if I could borrow someone else’s work. I found this gentleman had done the parsing of the CSV file already. Unfortunately for me, it was in Ruby, which I don’t know. (LIES!)

I hacked together some Python and, after some playing around with the data (and reading the above website), I found it was was three rows per day. The first row has some static data and variables which are the descriptions for the next two rows of data. The variables in the first row are the times it captures data points at. The second row had the data I wanted which was a value low (deep sleep) to high (light sleep).

A little more Python hackery and I produced Graphite friendly output.  The python script is available over here.

I was thinking of adding in the ability to put this directly into Graphite, but my mind was starting to wander at this point. Perhaps another day.