copy specific lines

Discussion in 'Perl Misc' started by Faith Greenwood, Sep 5, 2009.

  1. Hi,

    I am trying to split a file into two parts. My file is as follows:

    data
    1
    2
    3
    end data
    data
    4
    5
    6
    end data

    I want to split the file into two parts, w/ the first file having the
    following:
    data
    1
    2
    3
    end data

    and the second file having:
    data
    4
    5
    6
    end data

    I have the following code:

    open(SPLITTY, "<", "/media/hd/test.txt");
    my $linecnt=0;
    while(<SPLITTY>){
    if(/data/../end data/){
    open(YESSPLIT,">>","/media/hd/testtwo.txt");
    print YESSPLIT $_;
    close YESSPLIT;
    }
    }
    close SPLITTY;

    however, this just copies the original file to a new file. How can I
    tweak this to get what I am looking for?
     
    Faith Greenwood, Sep 5, 2009
    #1
    1. Advertising

  2. Faith Greenwood <> wrote:
    >I am trying to split a file into two parts. My file is as follows:
    >
    >data
    >1
    >2
    >3
    >end data
    >data
    >4
    >5
    >6
    >end data
    >
    >I want to split the file into two parts, w/ the first file having the
    >following:
    >data
    >1
    >2
    >3
    >end data
    >
    >and the second file having:
    >data
    >4
    >5
    >6
    >end data
    >
    >I have the following code:


    You should always use warnings and strict.

    >open(SPLITTY, "<", "/media/hd/test.txt");


    You should always check the success of open().

    >my $linecnt=0;


    What's that for? You never use that variable anywhere.

    >while(<SPLITTY>){

    print YESSPLIT $_;
    [old three lines snipped]
    if(/end data/){
    close YESSPLIT;
    open(Y ESSPLIT,">>","/media/hd/testtwo.txt") or
    errorhandling;
    }
    close YESSPLIT;
    >}
    >close SPLITTY;


    I think that should work.

    jue
     
    Jürgen Exner, Sep 6, 2009
    #2
    1. Advertising

  3. Faith Greenwood

    C.DeRykus Guest

    On Sep 5, 3:23 pm, Faith Greenwood <> wrote:
    > Hi,
    >
    > I am trying to split a file into two parts. My file is as follows:
    >
    > data
    > 1
    > 2
    > 3
    > end data
    > data
    > 4
    > 5
    > 6
    > end data
    >
    > I want to split the file into two parts, w/ the first file having the
    > following:
    > data
    > 1
    > 2
    > 3
    > end data
    >
    > and the second file having:
    > data
    > 4
    > 5
    > 6
    > end data
    >
    > I have the following code:
    >
    > open(SPLITTY, "<", "/media/hd/test.txt");
    > my $linecnt=0;
    > while(<SPLITTY>){
    > if(/data/../end data/){
    > open(YESSPLIT,">>","/media/hd/testtwo.txt");
    > print YESSPLIT $_;
    > close YESSPLIT;}
    > }
    >
    > close SPLITTY;
    >
    > however, this just copies the original file to a new file. How can I
    > tweak this to get what I am looking for?


    Hm, if you're trying to modify the original file itself,
    here's a possibility:


    local $^I = '.orig';
    local @ARGV = "/path/to/somefile";
    my $half;


    while ( <> ) {
    if( my $ret = /data/../end data/ and not $half ) {
    print;
    if ( index( $ret, "E0" ) > -1 ) {
    $half = 'done';
    open( my $fh, '>>', ... ) or die $!;
    }
    } elsif ( /data/../end data/ and $half ) {
    print $fh $_;
    }
    }




    --
    Charles DeRykus
     
    C.DeRykus, Sep 6, 2009
    #3
  4. Faith Greenwood

    C.DeRykus Guest

    On Sep 5, 8:07 pm, "C.DeRykus" <> wrote:
    > On Sep 5, 3:23 pm, Faith Greenwood <> wrote:
    >
    >
    >
    > ...
    >
    > Hm, if you're trying to modify the original file itself,
    > here's a possibility:
    >
    >     local $^I = '.orig';
    >     local @ARGV =  "/path/to/somefile";
    >     my $half;

    my $fh; # <------
    >
    >     while ( <> ) {
    >        if( my $ret = /data/../end data/ and not $half ) {
    >            print;
    >            if  ( index( $ret, "E0" ) > -1 ) {
    >               $half = 'done';
    >               open( my $fh, '>>', ... ) or die $!;

    open( $fh, '>>', ... ) or die $!; # <------
    >            }
    >        } elsif ( /data/../end data/ and $half ) {
    >            print $fh $_;
    >        }
    >     }
    >

    --
    Charles DeRykus
     
    C.DeRykus, Sep 6, 2009
    #4
  5. Faith Greenwood

    Guest

    On Sat, 5 Sep 2009 15:23:30 -0700 (PDT), Faith Greenwood <> wrote:

    >Hi,
    >
    >I am trying to split a file into two parts. My file is as follows:
    >
    >data
    >1
    >2
    >3
    >end data
    >data
    >4
    >5
    >6
    >end data
    >
    >I want to split the file into two parts, w/ the first file having the
    >following:
    >data
    >1
    >2
    >3
    >end data
    >
    >and the second file having:
    >data
    >4
    >5
    >6
    >end data
    >
    >I have the following code:
    >
    >open(SPLITTY, "<", "/media/hd/test.txt");
    >my $linecnt=0;
    >while(<SPLITTY>){
    >if(/data/../end data/){
    >open(YESSPLIT,">>","/media/hd/testtwo.txt");
    >print YESSPLIT $_;
    >close YESSPLIT;
    >}
    >}
    >close SPLITTY;
    >
    >however, this just copies the original file to a new file. How can I
    >tweak this to get what I am looking for?


    This is a way.
    However, this is too simple of an example and just
    demonstrates the range operator.
    Its a little more challenging that the data sets
    have identical header-footers.

    How would you use the fact that they are identical
    (not in data), in the real sense?
    Just curious.

    -sln
    -------------
    use strict;
    use warnings;

    (open my $fh1, ">data1.txt") && (open my $fh2, ">data2.txt")
    or die "couldn't open an output file: $!";
    my $fh = $fh1;

    while(<DATA>) {
    if ((/^data/../^end data/) =~ /\d+(E0|)?/) {
    print $fh "$_";
    $1 and $fh=$fh2;
    }
    }
    close $fh1;
    close $fh2;

    __DATA__

    data
    1
    2
    3
    end data
    data
    4
    5
    6
    end data
     
    , Sep 6, 2009
    #5
  6. Faith Greenwood

    Nathan Keel Guest

    wrote:

    > (open my $fh1, ">data1.txt") && (open my $fh2, ">data2.txt")
    > or die "couldn't open an output file: $!";


    What do you think that'll do, depending on if both fail?

    > my $fh = $fh1;
    >
    > while(<DATA>) {


    Your example uses __DATA__ when the user's question (and a good portion
    of their problem) specifically mentioned the issue with file
    modification. Just an FYI.
     
    Nathan Keel, Sep 7, 2009
    #6
  7. Faith Greenwood

    Guest

    On Mon, 07 Sep 2009 12:24:57 -0700, Nathan Keel <> wrote:

    > wrote:
    >
    >> (open my $fh1, ">data1.txt") && (open my $fh2, ">data2.txt")
    >> or die "couldn't open an output file: $!";

    >
    >What do you think that'll do, depending on if both fail?
    >

    If either fails, the less tight binding of 'or' reaches die.
    Both won't get evaluated unless the first one is true (&&).
    Therefore $! will be specific.

    1 && 0 or print "1 && 0 failed\n";
    0 && 1 or print "0 && 1 failed\n";
    1 && 1 or print "1 && 1 failed\n";
    0 && 0 or print "0 && 0 failed\n";
    print "end\n";
    ----
    1 && 0 failed
    0 && 1 failed
    0 && 0 failed
    end


    >> my $fh = $fh1;
    >>
    >> while(<DATA>) {

    >
    >Your example uses __DATA__ when the user's question (and a good portion
    >of their problem) specifically mentioned the issue with file
    >modification. Just an FYI.


    Two files with the correct data chunks.
    File modification is left as an excercise.

    -sln
     
    , Sep 7, 2009
    #7
  8. Faith Greenwood

    Nathan Keel Guest

    wrote:

    > On Mon, 07 Sep 2009 12:24:57 -0700, Nathan Keel <> wrote:
    >
    >> wrote:
    >>
    >>> (open my $fh1, ">data1.txt") && (open my $fh2, ">data2.txt")
    >>> or die "couldn't open an output file: $!";

    >>
    >>What do you think that'll do, depending on if both fail?
    >>

    > If either fails, the less tight binding of 'or' reaches die.
    > Both won't get evaluated unless the first one is true (&&).
    > Therefore $! will be specific.


    That's a weird design. Sure, it works the same way though.


    >>
    >>Your example uses __DATA__ when the user's question (and a good
    >>portion of their problem) specifically mentioned the issue with file
    >>modification. Just an FYI.

    >
    > Two files with the correct data chunks.
    > File modification is left as an excercise.
    >


    But that was his question, you didn't consider that.
     
    Nathan Keel, Sep 8, 2009
    #8
    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. Alex
    Replies:
    2
    Views:
    1,287
  2. =?Utf-8?B?SmF2?=

    Is ViwState Page-Specific or UserControl-Specific

    =?Utf-8?B?SmF2?=, Aug 16, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    571
    =?Utf-8?B?SmF2?=
    Aug 16, 2006
  3. Replies:
    26
    Views:
    2,175
    Roland Pibinger
    Sep 1, 2006
  4. mazdotnet
    Replies:
    2
    Views:
    424
    Alexey Smirnov
    Oct 2, 2009
  5. William FERRERES
    Replies:
    7
    Views:
    238
    William FERRERES
    Jul 9, 2007
Loading...

Share This Page