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

Hi, I've typical requirement and would like to have a perl script. The requirement is that input rows gets added five per line separated by comma. The input file might contain several rows but will have just one column. That means the perl script should interate down the line picking up five rows, joining them up in one line separated through comma. Here is something how it would like - Input File: abc.csv contains: monday june tuesday thread file said Expected Output File: abc.txt should contain: monday,june,tuesday,thread,file said, Any help in this regard will be appreciated. Thanks, Pamela Honeycutt

Replies are listed 'Best First'.
Re: Adding rows to column
by ikegami (Patriarch) on Jul 14, 2009 at 18:45 UTC

    Sounds like an identical question to Concatenate every 5 lines of input with a comma.. What follows is my earlier answer:


    As a one-liner:

    perl -ple'$\ = $. % 5 ? ";" : $/' infile > outfile

    In-place:

    perl -i.bak -ple'$\ = $. % 5 ? ";" : $/' file

    If it wasn't a one-liner,

    while (<$fh>) { chomp; print $_, $. % 5 ? ";" : $/; }

    PerlMonks formatting tip: Use <c>...</c> around computer text (code and data), and use <p> at the start of each paragraph.

      Thanks for the response. Any idea, why i'm getting this error : Global symbol "$fh" requires explicit package name at cgrpioneer.pl line 7. I even added the following line, but still the same. my $fh; Thanks, Pamela Honeycutt
        In the example there, $fh is the filehandle you want to read from.
        You should open my $fh, '<', "somefile.txt" or die $!; somewhere earlier in the program.

        PS: Simply saying that you added my $fh; doesn't help. You need to show the code, or the problem could be anything anywhere.