http://www.perlmonks.org?node_id=699400

unix_95054 has asked for the wisdom of the Perl Monks concerning the following question:

Folks,

I have a large file containing approx 1000+ file contents of similar type. The file looks like below:

<c> ================================== $ cat large_file < file1.txt line1 of file1 line2 of file1 line3 of file1 line4 of file1 < file2.txt line1 of file2 line2 of file2 line3 of file2 < file3.txt line1 of file3 line2 of file3 line3 of file3 line4 of file3 ==================================

I need to create files file1.txt, file2.txt and file3.txt from this large file. The format of actual file is exactly the same hence "<" can be used as file separator and "file1.txt" , "file2.txt" and "file3.txt" should be the outcome.

I can write a shell script using some complex awk/sed functions after devoting couple of hours :-) but I am sure that perl should have small and handy solution for this. Can anyone help me in this?

Thanks.

Replies are listed 'Best First'.
Re: How to generate files based on some special character?
by ikegami (Patriarch) on Jul 22, 2008 at 19:34 UTC
    my $out_fh; while (<$in_fh>) { if (my ($out_fn) = /^< (.*)/) { open($out_fh, '>', $out_fn) or die("Unable to create file \"$out_fn\": $!\n"); } else { print $out_fh $_; } }

    Update: Removed chomp.
    Update: Removed unneeded undef $out_fh;.

Re: How to generate files based on some special character?
by toolic (Bishop) on Jul 22, 2008 at 19:37 UTC
Re: How to generate files based on some special character?
by AltBlue (Chaplain) on Jul 22, 2008 at 19:47 UTC
    If you are very lazy, you could run this:
    $ perl -lane '$F[0] eq "<" and open O, "> $F[1]" or print O $_' large_ +file

    Obviously, I wouldn't use this at home, as it could burn it down ;-)

    ...because there are no error checks (on "file name", open and print) and because of the sloppy 2-arguments usage of open

    --
    altblue.

      Or be even more reckless and drop a few more keys ;-)
      $ perl -ne '/^< (.+)/ and open STDOUT, "> $1" or print' large_file
      God help you if you ever try to use such dumb code against some wild input ;-)

      --
      altblue.

Re: How to generate files based on some special character?
by poolpi (Hermit) on Jul 22, 2008 at 21:22 UTC

    Just for fun ;)

    #!/usr/bin/perl -w use strict; use autodie qw(open close); { undef $/; my %file = ($1 => split /<\s*(.+)\n/ , <DATA>); map { $_ and open my $fh, q{>}, $_; select $fh; print $file{$_} and close; } keys %file; } __DATA__ < file1.txt line1 of file1 line2 of file1 line3 of file1 line4 of file1 < file2.txt line1 of file2 line2 of file2 line3 of file2 < file3.txt line1 of file3 line2 of file3 line3 of file3 line4 of file3

    hth,
    PooLpi

    'Ebry haffa hoe hab im tik a bush'. Jamaican proverb
Re: How to generate files based on some special character?
by almut (Canon) on Jul 22, 2008 at 20:43 UTC

    Or (just for kicks):

    perl -pe"s/^</#EOF\ncat<<'#EOF'>/" large_file | sh