Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Re: How to scan a file, find a character string and print from that string to EOF

by myocom (Deacon)
on Aug 30, 2002 at 16:59 UTC ( #194177=note: print w/replies, xml ) Need Help??

in reply to How to scan a file, find a character string and print from that string to EOF

Without changing your code too much, you might do something like this. The basic idea is that you have a flag telling you whether you've hit DETS01 yet or not, then flip the flag when you hit it.

#!/usr/bin/perl -w open (INPUT, "target.txt") or die "can't read from target:$!\n"; open (OUTPUT1, ">file1.txt") or die "can't write to file1.txt:$!\n"; open (OUTPUT2, ">file2.txt") or die "can't write to file2.txt:$!\n"; # # my $firsthalf = 1; while (<INPUT>) { if ($_ =~ /^DETS01/) { $firsthalf = 0; } if ($firsthalf) { print OUTPUT1 $_; } else { print OUTPUT2 $_; } } close (INPUT); close (OUTPUT1); close (OUTPUT2);

Edit: Removed commas after filehandles in prints. Thanks, tadman!

"One word of warning: if you meet a bunch of Perl programmers on the bus or something, don't look them in the eye. They've been known to try to convert the young into Perl monks." - Frank Willison

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://194177]
[karlgoethebier]: shmem: something went wrong today?
[shmem]: karlgoethebier: of course
[shmem]: has there been any single day where nothing went wrong? not in my memory
[shmem]: discovered a fuel tank leakage at my second hand motorbike. Some bloke drove a screw too long through the fairing

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (8)
As of 2017-06-25 19:54 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (569 votes). Check out past polls.