http://www.perlmonks.org?node_id=1078810

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!! :(