Need Perl Script to Parse CSV File

Currently, I've been using Excel to cut and paste columns from one csv file to another by hand.  I would like to find a Perl Script that will do this for me or which I can easily modify.  I need to parse colums from file-1.csv and write to file-2.csv which has different column order and headings.  Does anyone know of such a Perl Script ?  I am aware of EZScripting CSVRead.pl and I'm looking at it.  Is anyone familiar with this script ?  Does it do what I need already or do I have to revise the code ?   Any other scripts you can suggest ?
Jerry LOperations ManagerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AtlantisstormCommented:
Hi,

There are two perl modules that make this kind of thing pretty simple.  Go to www.cpan.org and look up "Text::CSV" and "Text::CSV::Simple".  I can recommend both:)
FishMongerCommented:
Have you looked at the Text::CSV::Simple module.
http://search.cpan.org/~tmtm/Text-CSV-Simple-0.20/lib/Text/CSV/Simple.pm

Example (untested):

#!/usr/bin/perl -w

use strict;
use Text::CSV::Simple;

my ($parser, @data);
my $datafile = 'datafile.csv';
my $csvout = 'csvout.csv';

# Only want certain fields?
$parser = Text::CSV::Simple->new;
$parser->want_fields(1, 2, 4, 8);
@data = $parser->read_file($datafile);

open (CSV, "> $csvout") or die "can't open $csvout $!";
print CSV @data;
close CSV;

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
manav_mathurCommented:
what order do you want fields??
Could you give us a specific example??
Jerry LOperations ManagerAuthor Commented:
The destination file, file-2.csv will look like the following:  ( These are TAB Separated Fields; CR/LF after each 'EOREOR'.  To open this excerpt in Excel, you have to save to a file, then do a 'File -> Open' from Excel and use the import wizard. )

NOTE -- Only some of the source file fields will be used.  The two samples are not related, but the header fields are exactly what is expected for both files.

v_products_model      v_products_image      v_products_name_1      v_products_description_1      v_products_url_1      v_products_price      v_products_weight      v_date_avail      v_date_added      v_products_quantity      v_manufacturers_name      v_categories_name_1      v_categories_name_2      v_categories_name_3      v_categories_name_4      v_categories_name_5      v_categories_name_6      v_categories_name_7      v_tax_class_title      v_status      v_action      EOREOR
3780      CUBE-BURNOUT.jpg      BURNOUT - GAME CUBE      There's only one way to drive in Burnout: <B><FONT color=#ff0000>DANGEROUSLY. </FONT></B>The only high risk, high energy arcade style street racer available now for your next generation system. No learning curve required, just pick up and play! The most spectacular crashes ever - period. <I>Burnout's</I> action will leave you sweating, short of breath and totally in shock. Hyperventilating is part of the fun.<P><I>Burnout</I> monitors your driver's heart rate: you're rewarded for near-misses and the more risks you take. Civilians steer clear! </P>            29.95      0.45      2005-06-23 15:05:41      2005-06-23 15:05:41      1      ACCLAIM      Video Games      Game Cube                                    Taxable Goods      Active            EOREOR
5191      CUBENEEDSPDUNDR.jpg                        24.95      0.5      2005-06-23 15:05:41      2005-06-23 15:05:41      1      ELECTRONIC ARTS      Video Games      Game Cube                                    Taxable Goods      Active            EOREOR
4224      CUBE-ROCKETPWRB.jpg      ROCKET POWER BEACH BANDITS - GAME CUBE      From skateboarding to surfing to in-line skating, if it shreds, the Rocket Power gang is all over it. Now the extreme kids from the hit Nickelodeon cartoon arrive on the Nintendo GameCube in a 3D-platform adventure, Rocket Power Beach Bandits.<p><b>Like Sands Through the Hour Glass </b></p>            29.95      0.45      2005-06-23 15:05:41      2005-06-23 15:05:41      1      THQ      Video Games      Game Cube                                    Taxable Goods      Active            EOREOR

- SNIP -

Here is a sample Source File to be Parsed, File-1.csv:  ( Comma Separated Fields )

NOTE -- Header colums are from "Active", to "checkavail".  The first data field starts with the "T", and each successive record starts with the next "T".  Note that there is not a comma "," separating the last field between ROWS.  The "T" is the beginning of the next row.  It is a CR/LF, carriage return/linefeed.  If you save this to a .csv file and open it with Excel, all the columns line-up properly.

"Active","Date Added","Date Discontinued","CatID","Category","ParentCatID","ParentCat","Title","Item Code","itemID","Publisher","Packaging","Operating System","Weight","Stock","Wholesale", "Suggest_Retail","Image","short_desc","catlink", "popupcatlink","Description","imagelarge","esrp","upccode","checkavail"
"T","07/31/2003","","HOWTO","HOW TO GUIDES","REFERENC","REFERENCE","10,000 DRINK RECIPE - SNAP", "SNAP-10KDRINK","4038","TOPICS (L)","JEWEL CASE","WINDOWS 95/98/ME/NT/2000/XP",".350000","4.000000","3.75","9.95","SNAP-10KDRINK.jpg","<p>Trying to find the perfect after dinner drink? Not sure what you can mix with
the liquor you already have? Snap! Ô 10,000 Drink Recipes is the solution to all
your beverage needs.","http://www.catlink.info/iteminfo.asp?itemID=4038","<a target=""_blank"" href=""http://www.catlink.info/iteminfo.asp?itemID=4038"">SNAP-10KDRINK</a>","<p>Trying to find the perfect after dinner drink? Not sure what you can mix with the liquor you already have? Snap! Ô 10,000 Drink Recipes is the solution to all your beverage needs. From cocktails to bar drinks, you can find practically any recipe with the extensive search option. Providing an array of choices, this CD-ROM is a fantastic resource for any occasion. </p><p><b>System Requirements:</b></p><p>Windows 95/98/ME or better, 8MB RAM, 6MB HD, 2X CD-ROM drive, VGA Monitor, Sound Card</p>","SNAP-10KDRINK.jpg","","781735801284","<a target=""_blank"" href=""http://www.catlink.info/iteminfo.asp?itemID=4038"">Check Availability</a>"
"T","04/08/2004","","CLIP","CLIPART & PHOTOS","GRAPH","GRAPHICS & DESIGN","10,000 PHOTOS - SNAP","SNAP10KPHOTOS","4524","TOPICS (L)","JEWEL CASE","WINDOWS 95/98/ME/NT/2000/XP",".350000","23.000000","3.75","9.95","SNAP10KPHOTOS.jpg","Dress up your next newsletter, brochure, or presentation with selections from
Snap! 10,000 Photos, the expansive CD-ROM that brings a gallery of photography
to a desktop near you.","http://www.catlink.info/iteminfo.asp?itemID=4524","<a target=""_blank"" href=""http://www.catlink.info/iteminfo.asp?itemID=4524"">SNAP10KPHOTOS</a>","Dress up your next newsletter, brochure, or presentation with selections from Snap! 10,000 Photos, the expansive CD-ROM that brings a gallery of photography to a desktop near you. Conveniently arranged in alphabetical folders, the images run the gamut from Fireworks to Fruit &amp; Veggies, Animals to Architecture - all accessible for preview with a click of the mouse, making Snap! 10,000 Photos the ultimate software scrapbook! <p><b>Features:</b></p><ul>  <li>61 Category Folders from Agriculture to Winter</li>  <li>Thousands of Full-color Photos</li></ul><p><b>Snap!<br></b>Turn daily challenges into everyday solutions with Snap! Software. Let Snap! help you explore new topics with your PC. With great products at great prices, it's a Snap! </p><p><b>System Requirements for Windows:</b></p><ul>  <li>Pentium II or later</li>  <li>Windows 95 and higher</li>  <li>32MB RAM</li>  <li>20MB HD</li>  <li>CD-ROM drive</li>  <li>Graphics application that support JPEG files</li>  <li>Mouse</li></ul>","SNAP10KPHOTOS.jpg","","781735802182","<a target=""_blank"" href=""http://www.catlink.info/iteminfo.asp?itemID=4524"">Check Availability</a>"
 - SNIP -
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.