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

Re: doing a search and replace on a large file

by matija (Priest)
on Apr 14, 2004 at 12:30 UTC ( #345019=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://345019]
help
Chatterbox?
[LanX]: I remeber M::S (it was dialog driven?) to be buggy
[stevieb]: As far as Dist::Zilla goes, I don't like installing that other than on systems my test platorm runs on. I find it too heavy. I prefer being able to glean a Makefile.PL
[LanX]: what's frustrating me is that a distribution has lots of dupplicated info
[stevieb]: LanX I don't know if it's dialog driven; I just use it in the simplest of terms (just run module-starter at the CLI, and the very last couple of lines are how I use it.
[stevieb]: which dist are you speaking of regarding dups, LanX?
[LanX]: readme version number and so on ...
[stevieb]: with M::S, you can also add other tags, but defaults work... such as --license=perl --eumm
[LanX]: I'm not a big fan of pure make, apparently the auto generated ones are so complicated to be able to work with all possible makes

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2017-08-18 20:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Who is your favorite scientist and why?



























    Results (310 votes). Check out past polls.

    Notices?