Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Re: find a string and count of its occurence in a text file

by oha (Friar)
on Nov 14, 2007 at 10:26 UTC ( #650716=note: print w/replies, xml ) Need Help??

in reply to find a string and count of its occurence in a text file

As you did, you must open the file, then for every line of file you must check if starts with abc, then you can increment a variable or print out what you need.
open FILE, $file or die "can't open $file: $!\n"; while(<FILE>) { next unless /^abc:/; $counter++; chomp; print "$line\n"; # whatever you need } close FILE;
Doing this way you will never load all the file lines but parse one by one.
as someone noticed grep will work on arrays, so to use it you must load all the lines in one array @array = <FILE> which lead to memory issues if the file is big.


PS: perl have the poetry of next unless, which is so beauty instead of if(! COND) { continue } I can't avoid posting it! :)

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (6)
As of 2022-01-22 13:01 GMT
Find Nodes?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:

    Results (62 votes). Check out past polls.