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

I need to do a count of a specific word in a directory. I cant seem to get this count to work???
$data = "/dirpath"; opendir(DIR,$data) || die "$!"; @files = readdir(DIR); close(DIR); $number = 0; for(@files) { if($_ =~ /WORDHERE/gi) { $number++; } } print "total = $number\n";

Replies are listed 'Best First'.
Re: Count word in my dir
by blue_cowdawg (Monsignor) on Jul 31, 2003 at 16:02 UTC

    I am having a bit of trouble understanding what exactly you are trying to accomplish... but I'll try to help you anyway.

    How many times does the word "FOO" show up in a file name?

    There is are more ways to do this than you can shake a stick at (why you would go around shaking sticks at things I have no idea..) but I will propose a couple here.

    Shell method

    Perl method

        #!/usr/bin/perl -w use strict; use warnings; use diagnostics; my $path="/some/where/over/the/rainbow"; opendir(DIR,$path) or die "$!"; my @list=readdir(DIR); closedir(DIR); my @collect=grep /FOO/,@list; my $count = $#collect + 1; printf "total count = %d\n",$count;

    How many times does the word "FOO" show up in a file in my directory?

    Ahh... here is a better problem, one with a pair of fine solutions!

    Shell Method

    Perl Method

        #!/usr/bin/perl -w use strict; use warnings; use diagnostics; my $path="/some/where/over/the/rainbow"; open (DIR,$path) or die "$!"; my @file_list=readdir(DIR); closedir(DIR); my $count = 0; foreach my $file(@file_list){ open(FIN,"< $file") or die "$file: $!"; my @slurp=<FIN>; close(FIN); my @collect=grep /FOO/,@slurp; $count += ($#collect + 1); } printf "My total count was: %d\n",$count;

    A couple of things I should point out:

    • Using the die in conjunction with opendir and/or open can give you some idea if you are running into permission issues, path issues, fat fingerings and the like.
    • Likewise "use strict;", "use warnings;" and "use diagnostics"

    Hope this helps.


    Peter @ Berghold . Net

    Sieze the cow! Bite the day!

    Test the code? We don't need to test no stinkin' code! All code posted here is as is where is unless otherwise stated.

    Brewer of Belgian style Ales

Re: Count word in my dir
by dreadpiratepeter (Priest) on Jul 31, 2003 at 15:50 UTC
    The reason it isn't working is that you aren't opening the files. Readir returns a list of filenames. You have to open and read from each of the files.
    I'm assuming you are the poster of an almost identical question earlier today. The answer to this question is in the replies to the last question.
    Also, you are coding without strict or warnings. You cannot expect to learn to write good Perl code without them.


    -pete
    "Worry is like a rocking chair. It gives you something to do, but it doesn't get you anywhere."
Re: Count word in my dir
by broquaint (Abbot) on Jul 31, 2003 at 16:00 UTC
    If you're looking for a given word in a given directory then File::Find::Rule could be helpful e.g
    use File::Find::Rule; print "total = ", scalar find( file => maxdepth => 1, grep => qr/WORDHERE/, in => $data );
    This will find all the files in the $data directory, and search each file matching against the regex passed to grep, we then enforce scalar context on the return of find to get the count of the files that matched the criteria. See. File::Find::Rule for more info on this marvellous module.
    HTH

    _________
    broquaint

Re: Count word in my dir
by diotalevi (Canon) on Jul 31, 2003 at 16:37 UTC

    And now a vote for being succint:

    my $path = "/dirpath/*"; my $word = "foobar"; my $times = grep /\Q$wordhere/, glob $path; print "total = $times\n";
Re: Count word in my dir
by hardburn (Abbot) on Jul 31, 2003 at 15:41 UTC

    I don't see why that particular code wouldn't work. Though your for loop could be replaced with:

    $number = grep { /WORDHERE/i } @files;

    ----
    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    Note: All code is untested, unless otherwise stated

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Count word in my dir
by beernuts (Pilgrim) on Jul 31, 2003 at 15:49 UTC
    Works fine for me (once I added a shebang). You could also get this with cmd-line tools:

    ls -l | grep -i WORDHERE | wc -l

    Or, you could combine the readdir with a grep in perl:

    @files = grep /WORDHERE/,readdir(DIR);



    -beernuts
      This wont work, if the word appears two times in a single line...
      A more robust solution would be
      grep -h -i WORDHERE * | perl -le 'while(<>){$i+=s/\bWORDHERE\b//ig} pr +int $i'


      T I M T O W T D I
Re: Count word in my dir
by Grygonos (Chaplain) on Jul 31, 2003 at 15:41 UTC
    nm...I was thinking that it would matter what the regex would return ... like if you wanted to extract 99 from 359987. but A.M. was just wanting to test if 99 was in $_. my bad..
      That's just not correct.
      #!/usr/bin/perl -w use strict; foreach ("WORDHERE", "stuffthenWORDHERE", "WORDHEREthenstuff", "stuffbeforeWORDHEREstuffafter", "wordhere", "no_word_here") { print "$_: "; if($_ =~ /WORDHERE/gi) { print "matches"; } else { print "doesn't match"; } print "\n"; }
      produces
      WORDHERE: matches
      stuffthenWORDHERE: matches
      WORDHEREthenstuff: matches
      stuffbeforeWORDHEREstuffafter: matches
      wordhere: matches
      no_word_here: doesn't match