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

Dear monks!
I have a CSV that I generated from a MySQL table, now the CSV starts on row One and I need to add headers to the CSV file, so I have been trying to find the correct instruction to add one row in the CSV file. Can someone point me to the correct direction or if so kind to put an example I really apreciated thank you

Replies are listed 'Best First'.
Re: Insert blank rows in a CSV
by runrig (Abbot) on Jan 09, 2008 at 18:31 UTC
    perl -pi -e'print "some,csv,header\n" if $.==1' my_csv_file.csv
      Hi
      But for example if I want to leave a few rows in blank because i need to put a image there, how can I do that but not from command line but from a program
      this is what I am trying to tell you
      <---Start of the CSVFILE -----------------------------> <--Here goes a image ---------------------------------> <--Here goes a blank row -----------------------------> <--Here goes the header ------------------------------> 123456 123 john 9393939 data data 123456 123 john 9393939 data data 123456 123 john 9393939 data data 123456 123 john 9393939 data data 123456 123 john 9393939 data data
      So, Image I have already have it, but I cant tell the excel csv file to add rows
      How can i do that?
      thanks
        See How to add an image to Excel/CSV. I think you are out of luck doing it in a CSV, but there is an example in that node of getting the job done another way. As for adding rows in a CSV though (in a program not command line), just add the command line switches (-pi) to the #!perl line in the first line of the script, and the script would contain something similar to what is in the -e option in the command line example.
        Perhaps part of the confusion is that runnig's approach is to make a new copy of the file, adding some lines to the output stream at the appropriate places and then rename the new file to the original file name. So, the original file is not being edited in place, but a wholly new copy of the data is being made into a different file. When the new file is renamed to the orignal file name, it "becomes" the original file and the contents of the original file are automatically deleted (or, if you use the -i command line switch, the old file is renamed to another name so you can go back to it if necessary.)

        One nice property of this approach is that if your script bombs in the middle, you still have the original contents around. So, I hope this explains why you don't have to "reserve" space in your original file for the added content.

        On the other hand, if you want your original file to actually look like:

        <---Start of the CSVFILE -----------------------------> <--Here goes a image ---------------------------------> <--Here goes a blank row -----------------------------> <--Here goes the header ------------------------------> 123456 123 john 9393939 data data 123456 123 john 9393939 data data 123456 123 john 9393939 data data 123456 123 john 9393939 data data 123456 123 john 9393939 data data
        and you want the script to replace
        <--Here goes a image --------------------------------->
        with something, then that's a different problem, but you can still use the same idea:
        while (<>) { if (m/^<--Here goes a image.../) { print ...whatever... } else { print; } }
        Use the above code with the -i option to use perl's file editing feature. If the rows you want to replace are blank, then just use line numbers as the matching criteria:
        while (<>) { if ($. == 2) { print ...image csv line... } else { print; } }
        runrig's advice still applies.  print CSV_HANDLE ",,,,,\n"; would print a row with six empty cells to CSV_HANDLE file handle.