Parsing a flat file, how to create a csv file

I have a very large file that looks (more or less) like:

I: Steve
1234 Oak
Apt 5
City
I: Jack
99 Elm
Town
I: Dave
123 Pine
Suite 444
Mail stop 5
Village

And I want it to look like:

Steve, 1234 Oak, Apt 5, City
Jack, 99 Elm, Town
Dave, 123 Pine, Suite 444, Mail stop 5, Village

Each block of customer data begins with an "I:" and is followed by a name. Basically, if every "block" of data was the same number of lines, say 4, I'd just loop through the file and create a "record" for every four lines of the file. But in my file, some of the "blocks" are 3 lines and some are as much as 10. I know a new "block" begins with an "I:", but I cant figure out . . . well, hopefully you get the idea.

This is a one-time task, so a quick and dirty solution is fine.

Thanks
Steve
LVL 16
Steve JenningsIT ManagerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

svgmucCommented:
local $/='I:';
open FILE, "myfile" or die "Couldn't open file: $!";
binmode FILE;
while (<FILE>) {
    $record = $_;
    chomp $record;
    $record =~ s/\n/,/;
}
close FILE;
0
wilcoxonCommented:
This will work...
#!/usr/local/bin/perl

use strict;
use warnings;

my @data;
while (<DATA>) {
        chomp;
        if (/^I:\s+(.*)$/) {
                my $name = $1;
                if (@data) {
                        print join(', ', @data), "\n";
                        @data = ();
                }
                push @data, $name;
        } else {
                push @data, $_;
        }
}
if (@data) {
        print join(', ', @data), "\n";
}

__DATA__
I: Steve
1234 Oak
Apt 5
City
I: Jack
99 Elm
Town
I: Dave
123 Pine
Suite 444
Mail stop 5
Village

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
FishMongerCommented:
svgmuc has the right idea, but has a couple problems.

Try this:

#!/usr/bin/perl

use strict;
use warnings;

$^I = '.bak';
$/ = 'I: ';

@ARGV = 'file.txt';

while ( <> ) {
    chomp;
    my @fields = split /\n/;
    print join(',', @fields), "\n";
}

Open in new window

0
Steve JenningsIT ManagerAuthor Commented:
Thanks to all. Done and done.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.