how to split this file?

Discussion in 'Perl Misc' started by Huajian Luo, Nov 15, 2005.

  1. Huajian Luo

    Huajian Luo Guest

    Hi there,

    I need to split the following file to 3 parts that's
    ==>1) Found regular diskset containing disks: to first empty line
    ==>2) Found regular diskset containing disks: to first empty line
    .......
    the "Found regular diskset containing disks" maybe 1 or more than 1.
    ==>the Warning: message, the rest of the file

    Any hints on this?


    __DATA__



    1) Found regular diskset containing disks:
    c1t10d0
    c1t11d0

    Creation time: Mon Nov 14 20:32:17 2005
    For more information about this diskset:
    metaimport -r -v c1t10d0
    To import this diskset:
    metaimport -s <newsetname> c1t10d0


    2) Found regular diskset containing disks:
    c1t12d0

    Creation time: Mon Nov 14 20:33:10 2005
    For more information about this diskset:
    metaimport -r -v c1t12d0
    To import this diskset:
    metaimport -s <newsetname> c1t12d0



    Warning: The following disks have been detected in more than one set.
    Import recommendation based upon set creation time.
    Proceed with the import with caution.
    c1t11d0 - recommend importing with set created at : Mon Nov 14 22:06:48 2005


    ---end __DATA__


    --
    Thanks,

    Huajian Luo <>
     
    Huajian Luo, Nov 15, 2005
    #1
    1. Advertising

  2. Huajian Luo

    Guest

    Purl Gurl wrote:
    > [typical PG code]


    Is that code supposed to DO something? use Data::Dumper and put a
    print Dumper(\@Array);
    at the end of the program.
     
    , Nov 15, 2005
    #2
    1. Advertising

  3. Huajian Luo

    Huajian Luo Guest

    "Purl Gurl" <> writes:
    > Huajian Luo wrote:
    >
    > > I need to split the following file to 3 parts that's
    > > ==>1) Found regular diskset containing disks: to first empty line
    > > ==>2) Found regular diskset containing disks: to first empty line
    > > ......
    > > the "Found regular diskset containing disks" maybe 1 or more than 1.
    > > ==>the Warning: message, the rest of the file

    >
    > Yes, your "the rest of the file" is gibberish. Are readers to guess
    > at what "the rest of the file" contains? Why do you assume readers
    > to be psychic internet mind readers?
    >
    > A hint is, "Write articles which are clear, concise and coherent."

    I mean to split the __DATA__ to 3 parts

    > __DATA__
    >
    >
    > 1) Found regular diskset containing disks:
    > c1t10d0
    > c1t11d0
    >
    > Creation time: Mon Nov 14 20:32:17 2005
    > For more information about this diskset:
    > metaimport -r -v c1t10d0
    > To import this diskset:
    > metaimport -s <newsetname> c1t10d0


    This is Part 1
    >
    > 2) Found regular diskset containing disks:
    > c1t12d0
    >
    > Creation time: Mon Nov 14 20:33:10 2005
    > For more information about this diskset:
    > metaimport -r -v c1t12d0
    > To import this diskset:
    > metaimport -s <newsetname> c1t12d0

    This is the Part 2
    >
    >
    >
    > Warning: The following disks have been detected in more than one set.
    > Import recommendation based upon set creation time.
    > Proceed with the import with caution.
    > c1t11d0 - recommend importing with set created at : Mon Nov 14 22:06:48 2005



    And this is the rest of the file.

    --
    Thanks,

    Huajian Luo <>
     
    Huajian Luo, Nov 15, 2005
    #3
  4. Huajian Luo

    Guest

    Huajian Luo wrote:
    > ==>1) Found regular diskset containing disks: to first empty line
    > ==>2) Found regular diskset containing disks: to first empty line
    > ......
    > the "Found regular diskset containing disks" maybe 1 or more than 1.
    > ==>the Warning: message, the rest of the file


    This is a dreadful question (meaning it is very hard to ascertain your
    intent). The best way to get a good answer is to ask a good question.
    You have asked a very bad question, so you can only hope to get a very
    bad answer (as I believe PG has already provided).

    But, unlike PG, I am here to help you, not berate you. First of all,
    you should read the posting guidelines for this group. They can be
    found on-line at:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html

    These guidelines exist for YOUR benefit (because they show you how to
    compose effective posts which are much more likely to get effective
    responses - without getting flamed).

    >From what I was able to cut through the fog of the post, I believe

    something like this would work (although the criteria for the split is
    not robust - it can be easily broken by slight or unexpected changes in
    input format):

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

    my %section = (); #a hash of arrays, one array for each section
    my $part = 0; #which part is being seen now

    while (<DATA>) {
    $part++ if /^\d|^Warning/;
    push @{$section{$part}}, $_;
    }

    #verify the results
    for (1..3) {
    print "\n\n### THIS IS SECTION $_ ###\n",
    @{$section{$_}},
    "### End of Section $_ ###\n";
    }


    __DATA__
    1) Found regular diskset containing disks:
    c1t10d0
    c1t11d0

    Creation time: Mon Nov 14 20:32:17 2005
    For more information about this diskset:
    metaimport -r -v c1t10d0
    To import this diskset:
    metaimport -s <newsetname> c1t10d0

    2) Found regular diskset containing disks:
    c1t12d0

    Creation time: Mon Nov 14 20:33:10 2005
    For more information about this diskset:
    metaimport -r -v c1t12d0
    To import this diskset:
    metaimport -s <newsetname> c1t12d0

    Warning: The following disks have been detected in more than one set.
    Import recommendation based upon set creation time.
    Proceed with the import with caution.
    c1t11d0 - recommend importing with set created at : Mon Nov 14
    22:06:48 2005
     
    , Nov 15, 2005
    #4
  5. Huajian Luo

    Huajian Luo Guest

    writes:
    > But, unlike PG, I am here to help you, not berate you. First of all,
    > you should read the posting guidelines for this group. They can be
    > found on-line at:
    > http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
    >
    > These guidelines exist for YOUR benefit (because they show you how to
    > compose effective posts which are much more likely to get effective
    > responses - without getting flamed).


    Thanks and I'll check out it once I'm available.
    >
    >
    > #!/usr/bin/perl
    > use warnings; use strict;
    >
    > my %section = (); #a hash of arrays, one array for each section
    > my $part = 0; #which part is being seen now
    >
    > while (<DATA>) {
    > $part++ if /^\d|^Warning/;
    > push @{$section{$part}}, $_;
    > }
    >

    Yes, That's what I want, Thank your very much.

    --
    Thanks,

    Huajian Luo <>
     
    Huajian Luo, Nov 15, 2005
    #5
  6. <> wrote:
    > Purl Gurl wrote:
    >> [typical PG code]

    >
    > Is that code supposed to DO something?



    Yes, it is supposed impress the poor folks that don't know any better. :-(


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Nov 15, 2005
    #6
  7. Huajian Luo

    Guest

    Hi, Huajian:
    if you want to split your data into separated files, you can try the
    following code:
    ----
    #!/usr/bin/perl -w
    use strict;
    my $file=1;
    while(<DATA>) {
    if( /^\d\)\s+Found regular diskset containing disks/ ){
    open FH,">",sprintf("file_%03d.dat",$file++) or die "can't
    write, $!";
    select FH;
    } elsif ( /Warning:/) {
    open FH,"> warning.dat" or die "can't write Warning.dat, $!";
    select FH;
    }
    print;
    }
    close FH;
    ----
    Good luck,
    XC
     
    , Nov 15, 2005
    #7
  8. Huajian Luo

    Guest

    wrote:

    > #!/usr/bin/perl -w

    .... snip
    > while(<DATA>) {
    > if( /^\d\)\s+Found regular diskset containing disks/ ){
    > open FH,">",sprintf("file_%03d.dat",$file++) or die "can't write, $!";

    .... snip
    > }
    > close FH;


    Technically you only close the last file you opened. Perl should "clean
    up" on exit and close open files, but most hacks would agree that it's
    not good practice to do it this way.
     
    , Nov 15, 2005
    #8
  9. Huajian Luo

    Guest

    As I remember, in Perl, one can either explicitly close a filehandle by
    close(FH), or automatically close the filehandle by attaching the same
    filehandle to another file, i.e. using open(). coz I used the same name
    "FH" as the filehandle, so only one close() is necessary..
    Best,
    XC
     
    , Nov 15, 2005
    #9
  10. Huajian Luo

    Guest

    wrote:
    > As I remember, in Perl, one can either explicitly close a filehandle by
    > close(FH), or automatically close the filehandle by attaching the same
    > filehandle to another file...


    Yes, that's true. I should have looked at the code more closely before
    responding. Even so, my preference is to always do explicit file
    closures, but that's probably just a personal hang-up from days coding
    in other languages where explicit closure was essential (and failure to
    do so was disasterous).
     
    , Nov 15, 2005
    #10
    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. Replies:
    2
    Views:
    470
  2. Carlos Ribeiro
    Replies:
    11
    Views:
    702
    Alex Martelli
    Sep 17, 2004
  3. trans.  (T. Onoma)

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

    trans. (T. Onoma), Dec 27, 2004, in forum: Ruby
    Replies:
    10
    Views:
    218
    Florian Gross
    Dec 28, 2004
  4. Sam Kong
    Replies:
    5
    Views:
    246
    Rick DeNatale
    Aug 12, 2006
  5. Stanley Xu
    Replies:
    2
    Views:
    616
    Stanley Xu
    Mar 23, 2011
Loading...

Share This Page