copy specific lines

  • Thread starter Faith Greenwood
  • Start date
F

Faith Greenwood

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?
 
J

Jürgen Exner

Faith Greenwood said:
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
 
C

C.DeRykus

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 $_;
}
}
 
C

C.DeRykus

...

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 $!; # <------
 
S

sln

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
 
N

Nathan Keel

(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.
 
S

sln

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

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
 
N

Nathan Keel

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.

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

But that was his question, you didn't consider that.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top