Daily Archives: September 17, 2007

Using Ruby instead of Awk/SED/Grep/SH/Perl

I have been recently wondering if Ruby is a better language for script kiddie
stuff. The kind of stuff I use awk, sed, grep, csh, and sh to accomplish. Some crazy people use perl to do this. But some folks suggest that Ruby is good for this. This is a very different application from Rails – it is super-perl.

I read on the Internet somewhere that Matz had a plan to make a better and more consistent perl when he started Ruby and you can see that in its design and what it has out of the box.

I recently decided to switch to the iPhone (previous post) and had to switch from MeetingMaker to iCal / iPhone / Google Calendar. But all my contacts were in Meeting Maker – what to do?

Meeting Maker has a text export of the contacts – the file is tab delimited and they do line-ends DOS-style. The Apple Address Book takes VCARD format.

Usually faced with this, I write some sed, awk, and shell script stuff – but this looked a little too nasty for that. Usually when I am writing multiline awk programs in separate files to get the job done, I realize that I have gone too far. Often I fall into something like Java at that point and kind of have to start over – but ultimately I get it done in Java feeling kind of bad about it.

So this was the perfect high-motivation situation for me to try Ruby on the command line.

Short story isthat I really liked it – it is simple but powerful – want to make an object? Do it. Want to split lines – do it? Want to ready the whole file into a string and break it into an array of strings based on CRTL-M – do it.

All in all I ended up with a pretty nice Meeting Maker to VCard conversion script. I will likely clean it up, add some doc, and then release it – I include my basic version below.

Continue reading