something like this would work... you could shorten/lengthen it a bit
though. I would really like to know how this code can be improved..  
structurally... in the sense, that I want to learn how to do things the
'perl' way, and make that sort of thing instinctive..

I don't necessarily want obfuscated code... just neat/efficient ways to do 
things... pass the ketchup, please.

deen

#!/usr/bin/perl -w
use strict;

# usage: perl thisfile.pl infile outfile keyword
# Wed Jan 30 15:38:02 IST 2002 Deen Hameed 

open(FILENAME, $ARGV[0]) or die "Error: $! \n";
open(NEWFILE, ">$ARGV[1]") or die "Error: $! \n";

# print lines where the keyword does not occur
while ( <FILENAME> ) {
        last if ($_ =~ $ARGV[2]);
        print NEWFILE $_;
}

# process the line on which the keyword is found
if ($_ =~ $ARGV[2]) {

        my $word;
        
        foreach $word (split) {
                if ($word ne $ARGV[2]) {
                        print NEWFILE $word." ";
                } else {
                        print "Keyword found at line $. : $_\n";
                        exit;
                }
        }
}
__END__

--
Deen Hameedd, Accidental Programmer [EMAIL PROTECTED]

On Tue, 29 Jan 2002, Bill Akins wrote:

> Hi all,
> 
> I would like to read in a file and print out to a new file UNTIL I reach a
> key word in the file.
> 
> I tried something like
> 
> while (<>) {
> if !/KEYWORD/;
> print $ >> dest.txt;
> }
> 
> but that seems to copy all lines of the file except the one(s) contining
> KEYWORD.  How can I have this exit and close dest.txt when I reach the line
> containing KEYWORD?  Maybe
> 
> while (<>) {
> if /KEYWORD/;
> exit
> else print $ >> dest.txt;
> }
> 
> What would be the best approach?  Thanx for any help!
> 
> 
> 
> 



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to