Yes, it's possible, but it might be a little more work, as given the earlier requirements, you need to keep track of which sql statement is in which file.

It's almost easier to just write the code, than to try to explain the logic in english.

# this might be a memory hog for large files # read in the items to process my @items; { local $/ = '***** SQL Statement *****'; open ( INPUT, '<', $input_file_name ) or die ("Can't read from input : $!"); @items = <INPUT>; } # process each item foreach my $item ( @items ) { my ($sql, $other) = map { trim($_) } split ( /\Q***** Bind Variables + *****\E/, $item); my ($bindvars) = split( m/\n\n/, $other ); get_file($sql)->print("\n\n", $bindvars); } # clean up close_files(); # get rid of leading/trailing whitespace. sub trim { my $string = shift; $string =~ s/^\s+//; $string =~ s/\s+$//; return $string; } { use IO::File; my $i = 0; my %files = (); # return the file handle associated with a given sql statement. sub get_file { my $sql = shift; if ( defined( $files{$sql} )) { return $files{$sql}; } # we don't already know about it. my $file = IO::File->new( "SQL$i.txt", '>' ); if (!defined($file)) { die "Error opening output file: $!"; } $i++; $files{$sql} = $file; $file->print( $sql ); # print the sql statement as a header. return $file; } # close all of the open file handles sub close_files { foreach my $file ( values %files ) { $file->close(); } } }

of course, this is completely untested, but the logic should be good.


In reply to Re^3: Search a text and output to different text files by jhourcle
in thread Search a text and output to different text files by Anonymous Monk

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.