Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

how to search for a string in large file?

by littlemonk (Sexton)
on Mar 18, 2014 at 16:54 UTC ( #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
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? | Other CB clients
Other Users?
Others lurking in the Monastery: (1)
As of 2020-10-24 12:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favourite web site is:












    Results (243 votes). Check out past polls.

    Notices?