split text file?

Discussion in 'Perl Misc' started by pc, Jan 12, 2006.

  1. pc

    pc Guest

    hi everyone,

    I have a report that has three headings throughout the report. I want
    to split the report into three text files called section 1, section 2,
    section 3. is there a simple way of accomplishing this in perl?

    tia

    eddiec :)
    pc, Jan 12, 2006
    #1
    1. Advertising

  2. pc

    Guest

    pc wrote:
    > I want to split the report into three text files called section 1, section 2,
    > section 3. is there a simple way of accomplishing this in perl?


    Sure, piece-o-cake, especially with a little help from IO::All. This
    assumes, of course, that your "heading" is distinctive and won't occur
    within the main text. You didn't provide a sample of your headings or
    data, so I made something up based on comic-strip characters, which I
    read from __DATA__ for usenet convenience; adapt per your requirements:

    #!/usr/bin/perl
    use strict; use warnings;

    use IO::All;

    my $io; # [ IO::All handle ]
    while ( <DATA> ) {
    $io = io("/tmp/$1.txt") if /== (.*) ==/;
    $io -> print($_) if $io;
    }

    __DATA__
    == PEANUTS ==
    Charlie Brown
    Lucy

    == DILBERT ==
    Dilbert
    Wally
    Pointy-Haired Boss

    == GARFIELD ==
    Odie
    Jon

    --
    http://DavidFilmer.com
    , Jan 12, 2006
    #2
    1. Advertising

  3. pc <> wrote:

    > I have a report that has three headings throughout the report. I want
    > to split the report into three text files called section 1, section 2,
    > section 3. is there a simple way of accomplishing this in perl?



    Yes.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Jan 12, 2006
    #3
  4. pc

    Xicheng Guest

    you can do it on the command line:
    perl -ne 'if(/=heading pattern=/) {$n++;open FH,">","section $n"}
    print FH;END{close FH}' file.txt
    or if you don't need the heading into your splited files:
    perl -ne 'if(/=heading pattern=/) {$n++;open FH,">","section
    $n";next} print FH;END{close FH}' file.txt

    At least for some small-sized files, the "END{close FH}" statement can
    often be neglected.
    Good luck,
    Xicheng

    pc wrote:
    > hi everyone,
    >
    > I have a report that has three headings throughout the report. I want
    > to split the report into three text files called section 1, section 2,
    > section 3. is there a simple way of accomplishing this in perl?
    >
    > tia
    >
    > eddiec :)
    Xicheng, Jan 12, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Krish
    Replies:
    1
    Views:
    1,064
    =?Utf-8?B?Q3VydF9DIFtNVlBd?=
    Oct 20, 2005
  2. Replies:
    2
    Views:
    457
  3. Carlos Ribeiro
    Replies:
    11
    Views:
    689
    Alex Martelli
    Sep 17, 2004
  4. trans.  (T. Onoma)

    split on '' (and another for split -1)

    trans. (T. Onoma), Dec 27, 2004, in forum: Ruby
    Replies:
    10
    Views:
    204
    Florian Gross
    Dec 28, 2004
  5. Sam Kong
    Replies:
    5
    Views:
    231
    Rick DeNatale
    Aug 12, 2006
Loading...

Share This Page