Dear most gracious and benevolent monks,

I beseach your grace and ask that you examine my humble supplication.

I am trying to go through a sorted text file, which are a bunch of comma seperated fields of very long lines (80+++ characters). Here is an simplified example:

Dog, Table, 12:30, Mercury,abcdef Dog, Chair, 5:30, Mercury,abcdef Dog, Table, 12:30, Venus,abcdef Pig, Door, 3:30, Earth, 123 Pig, Door, 3:30, Earth, 45678943985 Goat, Couch, 8:45, Mars, 9876

I would like every line that has the same 1st variable to be combined into one text file named after the 1st field. All 1st fields would have a file named after it. For the above example which has 3 different 1st fields I would have 3 text files like so:

{Dog.txt} Dog, Table, 12:30, Mercury,abcdef Dog, Chair, 5:30, Mercury,abcdef Dog, Table, 12:30, Venus,abcdef {Pig.txt} Pig, Door, 3:30, Earth, 123 Pig, Door, 3:30, Earth, 45678943985 {Goat.txt} Goat, Couch, 8:45, Mars, 9876
Here is code I have written so far.
#!/usr/bin/perl -w use strict; my $sourcefile = $ARGV[0]; my $currentline; open(my $fh, '<', $sourcefile) or die ("Could not open $sourcefile"); #gets the filename from the cmd line while(my $line = <$fh>) #until EOF, do the following { my $nextline =' '; #initialize the next line to empty before starting foreach my $line (<$fh>) #for every line go in { chomp($line); my @currentline = split(/,/, $line); #split the fields next if (($currentline[0]) eq (@nextline[0])); #if 1st field in currentline = 1st field in the 2nd li +ne open OUT, ">> $currentline[0].txt" or die "Could not open file $currentline[0].txt for outpu +t.\n$!"; #create the filename based upon the first field conten +ts #if the file already exists, append to it print OUT $currentline; #throw everything in that particular line into the fil +e $nextline = $currentline; #go to the next line close OUT; #close the file } }
I do beg for you assistance and fully prostrate my self in gratitude to those who are so wise and charitable.

In reply to Aggregating Lines from a CSV who lines match a particular field and save those matches based on that matching field name by Knoperl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.