How can I do this?

What Anonymous Monk says. But that's probably too dense at first glance. So let's go step by step.
You have a well formed input file containing two fields separated by ' : '.
The first field is data, the second field denominates the destination file.
To write the data portion to the destination, you have to open/create a file for writing, for the respective destination. See open.
While processing each line, you want to separate the data from the destination denominator. See split.
Now, how can you get the filehandle to print at from the destination denominator? You store the filehandle in a hash. See perldata.
A hash can store filehandles as values to be retrieved via a string. Since you have to retrieve the destination for each line of input, you need not open the files beforehand, since you can combine the retrievement of a filehandle from the hash with a logical or: if it doesn't exist, you open the file and store the filehandle in the filehandle hash under the respective destination key.

How could you put that into code? Well, first let's set up a filehandle hash containing the tokens and filehandles for the destination. See my.

my %fh;

Reading a file given as an argument at the command line is easy:

while(<>) { # the line read is stored into $_ chomp; # remove line ending character from $_ ... }

See readline for the diamond operator <>. See chomp.
Split the line based on your separator (see perlre, perlop and perlfunc):

my( $data, $fh_token ) = split /\s*:\s*/;

Now, get the filehandle for $fh_token, and if it doesn't exist, create one and store the filehandle into %fh

if( ! $fh{$fh_token}) { open my $fh, '>', "$fh_token.txt" or die "Can't write to '$fh_token.txt': $!\n"; $fh{$fh_token} = $fh; # store filehandle } my $fh = $fh{$fh_token}; # this is the filehandle to print to

Then print the data stuff to the determined filehandle:

print $fh $data, "\n"; # append a line break. You'd use "\r\n" on +windows.

So, we have:

my %fh; while(<>) { # the line read is stored into $_ chomp; # remove line ending character from $_ my( $data, $fh_token ) = split /\s*:\s*/; if( ! $fh{$fh_token}) { open my $fh, '>', "$fh_token.txt" or die "Can't write to '$fh_token.txt': $!\n"; $fh{$fh_token} = $fh; # store filehandle } my $fh = $fh{$fh_token}; print $fh $data, "\n"; # append a line break. You'd use "\r\n" on +windows. }

The code by Anonymous Monk above makes use of features built into perl. See perlrun and IO::File. Condensation of this code to resemble the succinct variation is left as an excercise to the reader. TIMTOWTDI (there's more than one way to do it).

perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'

In reply to Re: Break a file into separate files based on string match by shmem
in thread Break a file into seperate files based on string match by hvirani

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.