[mdlug] csv files

Robert Citek robert.citek at gmail.com
Mon Dec 7 16:34:28 EST 2009


On Mon, Nov 16, 2009 at 10:19 PM, Dan Pritts <danno at umich.edu> wrote:
> On Sat, Nov 14, 2009 at 07:29:01AM -0500, Carl T. Miller wrote:
>> If anyone does know of any other command line utilities, I'm
>> all ears.
>
> I don't have the code in front of me but i've used some relevant
> CPAN modules with success.
>
> danno

Ruby works very nicely, too.  An example from "Programming Ruby: The
Pragmatic Programmer's Guide" (aka the pickaxe book):

$ cat csvfile
Count, Description, Price
12,eggs,2.89,
2,"shirt, blue",21.45,special
1,"""Hello Kitty"" bag",13.99

$ ruby -e 'require "csv"
CSV.open("csvfile","r") do |row|
  qty = row[0].to_i
  price = row[2].to_f
  printf "%20s: $%5.2f %s\n", row[1], qty*price, row[3] || " ---"
end'
         Description: $ 0.00  ---
                eggs: $34.68  ---
         shirt, blue: $42.90 special
   "Hello Kitty" bag: $13.99  ---

The above was run using Ruby 1.8.6 on Ubuntu 8.04.

Regards,
- Robert



More information about the mdlug mailing list