Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
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?
[Corion]: erix: Sure, but SQLite doesn't even need that :)
[hippo]: Corion: Yes, you can. It's all in the pg_hba.conf
[erix]: yeah, but there is a lot SQLite cannot do :)
[hippo]: Database is the 2nd column.
shmem .oO( docker. docker. docker? docker. docker! docker )
[shmem]: at least, some Ook.
[erix]: another simple Pg thing is to running a database off a tsv (alas, readonly)
[erix]: I'm waiting for that zipfile-reading to show up in DBD::SQLite :)
[Corion]: hippo, erix: That sounds promising :)
[Corion]: erix: I misinterpreted that. It only allows to read a zipfile like a table, not reading a database from a zipfile :-(

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (11)
As of 2018-02-23 15:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    When it is dark outside I am happiest to see ...














    Results (302 votes). Check out past polls.

    Notices?