Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Perl script to find particular string and save those lines in another file?

by graff (Chancellor)
on May 05, 2015 at 05:24 UTC ( [id://1125668]=note: print w/replies, xml ) Need Help??


in reply to Perl script to find particular string and save those lines in another file?

You seem to be opening the "NEW" file handle after you've been trying to write to it. Trying opening "NEW" before the "for" loop.

(Also, it's better to do it like this:

use strict; my $find = "2"; open( NEW, ">", "new.txt" ) or die "..." open( FILE, "<", "high_fanout.txt") or die "..." while (<FILE>) { print NEW if ( /$find/ ); }
(updated to "use strict" and add "my" for the one variable)
  • Comment on Re: Perl script to find particular string and save those lines in another file?
  • Download Code

Replies are listed 'Best First'.
Re^2: Perl script to find particular string and save those lines in another file?
by sumathigokul (Acolyte) on May 05, 2015 at 06:11 UTC

    Thank you, this code works.

    use strict; my $find = "2"; open (NEW, ">", "new.txt" ) or die "could not open:$!"; open (FILE, "<", "high_fanout.txt") or die "could not open:$!"; while (<FILE>) { print NEW if (/$find/); } close (FILE); close (NEW);

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1125668]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2024-07-20 01:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.