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

how to search for a string in large file?

by littlemonk (Sexton)
on Mar 18, 2014 at 16:54 UTC ( [id://1078810]=perlquestion: print w/replies, xml ) Need Help??

littlemonk has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, This was a question asked in a job interview. What is the best way to search the number of occurrences of a string in a very large text file (>1 gb)..please help me!
  • Comment on how to search for a string in large file?

Replies are listed 'Best First'.
Re: how to search for a string in large file?
by NetWallah (Canon) on Mar 18, 2014 at 17:28 UTC
    Linux: grep -c "string" Very-large.file Win: find /C "string" File-name.txt Perl: perl -ne "$x++ if m/string/}{print qq|$x\n|" Large-file.txt (Change to single quote for Linux)

            What is the sound of Perl? Is it not the sound of a wall that people have stopped banging their heads against?
                  -Larry Wall, 1992

Re: how to search for a string in large file?
by thezip (Vicar) on Mar 18, 2014 at 16:59 UTC
     grep <string> <filename> | wc -l


    *My* tenacity goes to eleven...
Re: how to search for a string in large file?
by oiskuu (Hermit) on Mar 18, 2014 at 18:22 UTC

    perl -ne '$n += () = /foo/g }{ print $n' <(< large.txt fgrep foo)
    Though this might not cover them all. Should "foof" match "foofoof" once or twice?

      could please explain me the answer .. im new to perl!! :(

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (2)
As of 2024-04-26 07:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found