http://qs1969.pair.com?node_id=345019


in reply to doing a search and replace on a large file

Just about the way you outlined:
open(INPUT,"<$bigfile") ||die "Could not open $bigfile:$!\n"; open(OUTPUT,">$newfile")||die "Could not open $newfile:$!\n"; while (<INPUT>) { print OUTPUT $_ # note no semicolon here... unless /whatever condition matches the patterns you want/; } close(INPUT) || die "Could not close $bigfile: $!\n"; close(OUTPUT)|| die "Could not close $newfile: $!\n";

Replies are listed 'Best First'.
Re: doing a search and replace on a large file
by b10m (Vicar) on Apr 14, 2004 at 12:48 UTC

    Minor tweak: since the OP wants to "delete" lines that do not match a certain pattern, I'd change the unless into an if. That way, you "save" the lines that do match the pattern ;-)

    open IN, "<$bigfile" or die "Can't open $bigfile: $!\n"; open OUT, ">$newfile" or die "Can't open $newfile: $!\n"; while(<IN>) { print OUT $_ if /the pattern in question/; } close OUT; close IN;
    --
    b10m

    All code is usually tested, but rarely trusted.
Re: Re: doing a search and replace on a large file
by aquarium (Curate) on Apr 14, 2004 at 14:05 UTC
    i thought what was needed was to (A) delete matching lines in file...and (B) write the matching lines to another file. If instead you could read the file and produce two output files, one file for the matching lines, and one file for the rest (unmatched lines), then the objective can be achieved with a slight modification to matija code like so:
    open(OUTPUT,">matchingfile.txt")||die; open(OUTPUT2,">nonmatchingfile.txt")||die; while (<>) { chomp; if(/what you want to match/) { print OUTPUT $_ . "\n"; } else { print OUTPUT2 $_ . "\n"; } print OUTPUT $_ # note no semicolon here... unless /whatever condition matches the patterns you want/; } close(OUTPUT)|| die "Could not close $!\n"; close(OUTPUT2)|| die "Could not close $!\n";
    ...and run the prog like this: perl prog.pl <yourinputfile