{"id":367,"date":"2007-09-17T12:24:39","date_gmt":"2007-09-17T16:24:39","guid":{"rendered":"http:\/\/www.dr-chuck.com\/wordpress\/?p=367"},"modified":"2011-12-17T12:23:53","modified_gmt":"2011-12-17T16:23:53","slug":"using-ruby-instead-of-awksedgrepshperl","status":"publish","type":"post","link":"https:\/\/www.dr-chuck.com\/csev-blog\/2007\/09\/using-ruby-instead-of-awksedgrepshperl\/","title":{"rendered":"Using Ruby instead of Awk\/SED\/Grep\/SH\/Perl"},"content":{"rendered":"<p>\nI have been recently wondering if Ruby is a better language for script kiddie<br \/>\nstuff.  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 &#8211; it is super-perl.<\/p>\n<p>\nI 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.<\/p>\n<p>\nI 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 &#8211; what to do?<\/p>\n<p>\nMeeting Maker has a text export of the contacts &#8211; the file is tab delimited and they do line-ends DOS-style.  The Apple Address Book takes VCARD format.<\/p>\n<p>\nUsually faced with this, I write some sed, awk, and shell script stuff &#8211; 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 &#8211; but ultimately I get it done in Java feeling kind of bad about it.<\/p>\n<p>\nSo this was the perfect high-motivation situation for me to try Ruby on the command line.<\/p>\n<p>\nShort story isthat I really liked it &#8211; it is simple but powerful &#8211; want to make an object?  Do it.  Want to split lines &#8211; do it?  Want to ready the whole file into a string and break it into an array of strings based on CRTL-M &#8211; do it.\n<\/p>\n<p>\nAll 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 &#8211; I include my basic version below.\n<\/p>\n<p><!--more--><\/p>\n<pre>\n#!\/usr\/bin\/ruby\n# how many columns we expect\nMAX = 25\nFIRST = 0\nLAST = 1\nTITLE = 4\nCOMPANY = 6\nEMAIL = 12\nHOME = 13\nWORK = 14\nCELL = 15\nNOTES = 25\n# These are assumed to be contiguous and in the\n# right order, see below\nADDR = 7\nCITY = 8\nSTATE = 9\nZIP = 10\nCOUNTRY = 11\nrecords = []\nlines = []\n#depending on line end - this will either get the\n# whole file or the first line\nfirstline = gets\nif firstline\nsecondline = gets\nif secondline\n# puts \"Reading lines from input ...\"\nlines << firstline\nlines << secondline\nwhile gets\nlines << $_\nend\nelse\n# split the lines basedon newline CTRL-M\nlines = firstline.split(\"\\015\");\n# puts \"Split lines based on newline\"\nend\n# puts \"Lines read: \" + lines.size.to_s\nelse\nputs \"No Input - nothing to process\"\nend\n# Ignore the first three lines -\n# the first is a blank line\n# the second is \"Contacts\" and\n# the third is the Column headings\n3.upto(lines.size - 1 ) { |recpos|\n# puts recpos.to_s + lines[recpos].to_s\nrecord = lines[recpos].split(\"\\011\")\n# puts record.size\n# Set all nils to empty string to simplify code below\n0.upto(MAX) { |j|\nif record[j] == nil\nrecord[j] = \"\"\nend\nrecord[j] = record[j].gsub('\\n',' ')\nrecord[j] = record[j].strip\n# print j,\" \",record[j],\"\\n\"; }\n}\n# Insist on at least a first or last name\nfname = record[FIRST]\nlname = record[LAST]\n# no need to continue...\nif  fname.empty? and lname.empty?\n# puts \"Skipping \"+i.to_s\nnext\nend\nprint \"BEGIN:VCARD\\n\"\nprint \"VERSION:3.0\\n\"\n# \"FN:AAA First AAA Last\\n\"\nprint \"N:\"\nprint lname unless lname.empty?\nprint \";\"\nprint fname unless fname.empty?\nprint \";;;\\n\"\nprint \"FN:\"\nprint fname unless fname.empty?\nprint \" \" unless ( fname.empty? || lname.empty? )\nprint lname unless lname.empty?\nprint \"\\n\";\nunless record[COMPANY].empty?\nprint \"ORG:\"+record[COMPANY]+\";\\n\"\nend\nunless record[TITLE].empty?\nprint \"TITLE:\"+record[TITLE]+\";\\n\"\nend\n# print \"EMAIL;type=INTERNET;type=WORK;type=pref:email@work.com\\n\"\nunless record[EMAIL].empty?\nprint \"EMAIL;type=INTERNET;type=WORK;type=pref:\"+record[EMAIL]+\"\\n\"\nend\n# print \"TEL;type=WORK;type=pref:734-work\\n\"\nunless record[WORK].empty?\nprint \"TEL;type=WORK;type=pref:\"+record[WORK]+\"\\n\"\nend\nunless record[HOME].empty?\nprint \"TEL;type=HOME;type=pref:\"+record[HOME]+\"\\n\"\nend\nunless record[CELL].empty?\nprint \"TEL;type=CELL;type=pref:\"+record[CELL]+\"\\n\"\nend\nunless record[ADDR].empty? &#038;&#038; record[CITY].empty?\n&#038;&#038; record[STATE].empty? &#038;&#038; record[ZIP].empty?\n&#038;&#038; record[COUNTRY].empty?\nprint \"item1.ADR;type=WORK;type=pref:;;\"\n# Assume they are contiguous\nADDR.upto(COUNTRY) { |pos|\nfield = record[pos]\nprint field unless field.empty?\nprint \";\"\n}\nprint \"\\n\"\nend\nprint \"END:VCARD\\n\"\n}\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-367","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.dr-chuck.com\/csev-blog\/wp-json\/wp\/v2\/posts\/367","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dr-chuck.com\/csev-blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dr-chuck.com\/csev-blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dr-chuck.com\/csev-blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dr-chuck.com\/csev-blog\/wp-json\/wp\/v2\/comments?post=367"}],"version-history":[{"count":1,"href":"https:\/\/www.dr-chuck.com\/csev-blog\/wp-json\/wp\/v2\/posts\/367\/revisions"}],"predecessor-version":[{"id":2450,"href":"https:\/\/www.dr-chuck.com\/csev-blog\/wp-json\/wp\/v2\/posts\/367\/revisions\/2450"}],"wp:attachment":[{"href":"https:\/\/www.dr-chuck.com\/csev-blog\/wp-json\/wp\/v2\/media?parent=367"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dr-chuck.com\/csev-blog\/wp-json\/wp\/v2\/categories?post=367"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dr-chuck.com\/csev-blog\/wp-json\/wp\/v2\/tags?post=367"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}