dolo21taf has asked for the wisdom of the Perl Monks concerning the following question:

Hi Infamous PerlMonks,


I want to add 3 different columns to the file in which:

1. The first new column pulls in today's date and time
2. Second column one has a '0'

3. Third column has the word 'ANY' going down the column
If my file content is as follows:


"7","a","abc",123"

"8","b","cde","456"

"9","c","fgh","789"


This is what I want the file content to be:

"2008-04-03 10:33:08",0,ANY,"7","a","abc",123"

"2008-04-03 10:33:08",0,ANY,"8","b","cde","456"

"2008-04-03 10:33:08",0,ANY,"9","c","fgh","789"

Big brownie points in advance......and a bigger thanks

  • Comment on Adding columns to CSV file and adding info under each column

Replies are listed 'Best First'.
Re: Adding columns to CSV file and adding info under each column
by chromatic (Archbishop) on Mar 05, 2008 at 06:55 UTC
    perl -pi.bak -e 's/^/"2008-04-03 10:33:08",0,ANY,/' *.csv
Re: Adding columns to CSV file and adding info under each column
by Corion (Patriarch) on Mar 05, 2008 at 06:58 UTC

    This is a really trivial task to me, as it seems all you want to do is to prepend a constant string to every line. But as I think the solution is obvious, it's likely that I'm solving the wrong problem here.

    So, what code do you already have? Where do you have problems?

Re: Adding columns to CSV file and adding info under each column
by GrandFather (Saint) on Mar 05, 2008 at 08:39 UTC

    Depending on what you really need to do and how big the task is it may be worth converting the csv file to a database, or at least using DBD::AnyData along with DBI to wrap your csv file up as though it were a database. Column manipulations then become rather straight forward.


    Perl is environmentally friendly - it saves trees