Knoperl has asked for the wisdom of the Perl Monks concerning the following question:
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:
Here is code I have written so far.{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
I do beg for you assistance and fully prostrate my self in gratitude to those who are so wise and charitable.#!/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 } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Aggregating Lines from a CSV who lines match a particular field and save those matches based on that matching field name
by ikegami (Patriarch) on Dec 10, 2008 at 05:56 UTC | |
|
Re: Aggregating Lines from a CSV who lines match a particular field and save those matches based on that matching field name
by NetWallah (Canon) on Dec 10, 2008 at 06:08 UTC | |
by ikegami (Patriarch) on Dec 10, 2008 at 06:19 UTC | |
by Knoperl (Acolyte) on Dec 10, 2008 at 23:04 UTC |