Perl - Count occurences

I have enclosed the script. I am able to find the files that contain my search string but when I try to count the occurences within the file I get zero always. Any help on this.

#!/usr/bin/perl
my $find = $ARGV[0];
my $replace = $ARGV[1];
my $glob = $ARGV[2];
@filelist = <*$glob>;

# process each file in file list
foreach $filename (@filelist) {
$count = 0;
# print " P: $filename\n";
# retrieve complete file
open (IN, "$filename") || die("Error Reading File: $filename $!");
{
undef $/;
$infile = <IN>;
}
close (IN) || die("Error Closing File: $filename $!");
if ($infile =~ m/$find/gio)
{ print " Matched: $filename\n";
while (<IN>) {
while (m/$find/gio) {
$count++;
}
print $ARGV . "contains " . $search_this . " " . $count . (($count == 1) ? " time\n" : " times\n");
}
}
}
print "\nFinished.\n";

exit(0);

Code tags:

#!/usr/bin/perl
my $find = $ARGV[0];
my $replace = $ARGV[1];
my $glob = $ARGV[2];
@filelist = <*$glob>;
 
# process each file in file list
foreach $filename (@filelist) {
       $count = 0;
# print "    P: $filename\n";
# retrieve complete file
    open (IN, "$filename") || die("Error Reading File: $filename $!");
   {
    undef $/;          
    $infile = <IN>;
   }
    close (IN) || die("Error Closing File: $filename $!");
   if  ($infile =~ m/$find/gio){
            print "    Matched: $filename\n"; 
            while (<IN>) {
                  while (m/$find/gio) {
                  $count++;
                   }
    print $ARGV . "contains " . $search_this . " " . $count . (($count == 1) ? " time\n" : " times\n");
    }
   }
}
   print "\nFinished.\n";

   exit(0);

code tags for code please, they make it readable. (as far as that goes for perl, anyhow.)

[ code ] stuff [ / code ] without the extra spaces in the tags.

I am new to perl and do not understand your answer. Can you tell me specifically the error in my code?

I added code tags to your code. I think there is a { .. } mismatch in your example.

I still get no counts

#!/usr/bin/perl

my $find = $ARGV[0];
my $replace = $ARGV[1];
my $glob = $ARGV[2];
@filelist = <*$glob>;

# process each file in file list
foreach $filename (@filelist) {
$count = 0;
# print " P: $filename\n";
# retrieve complete file
open (IN, "$filename") || die("Error Reading File: $filename $!");
{
undef $/;
$infile = <IN>;
}
close (IN) || die("Error Closing File: $filename $!");
if ($infile =~ m/$find/gi)
{
while (<IN>) {
while ($infile =~ m/$find/gi)
{$count++};
}
print $filename . " contains " . $find . " " . $count . (($count == 1) ? " time\n" : " times\n");
}
}
print "\nFinished.\n";

exit(0);

I'm not being funny is there a requirement not to do this:

grep -c 'search string' file file1 file2

I was not aware I could pick unix commands within perl. If I can then of course I can use the grep.

Thanks

perl can use system commands, yes. if that's all your perl script needs to do though, why use perl at all?

The other day this forum assisted me with my complex find statement in Unix with my grep, but I have reached the limit of the command line and therefore started to recode in Perl.

My understanding is that perl is much faster, also.

What is the syntax for Grep in Perl.. It is a built in caommand and different from unixs syntax

Type in

perldoc -f grep

But I don't think it is exactly what you want. You want to search through files while the grep command looks through lists.

You may also want to check this out:

perlfaq

I think he means:

$/usr/bin/perl
print `/usr/bin/grep -c "searchstring"  @ARGV`   

perl has its own grep function and there is no doubt in my mind this is school work, which is not allowed to be posted here. Here is a big problem in the code you posted:

    open (IN, "$filename") || die("Error Reading File: $filename $!");
   {
    undef $/;          
    $infile = <IN>; <-- HERE THE FILE IS READ
   }
    close (IN) || die("Error Closing File: $filename $!");
   if  ($infile =~ m/$find/gio){
            print "    Matched: $filename\n"; 
            while (<IN>) {<-- HERE THERE IS NOTHING TO READ 
                  while (m/$find/gio) {
                  $count++;
                   }
    print $ARGV . "contains " . $search_this . " " . $count . (($count == 1) ? " time\n" : " times\n");
    }
   }
}

See if you can figure it out. I don't do peoples school work for them.

Now I just need to figure out how to get complete file path to print out as well.

#!/usr/bin/perl

my $find = $ARGV[0];
my $replace = $ARGV[1];
my $glob = $ARGV[2];
@filelist = <*$glob>;

# process each file in file list
foreach $filename (@filelist) {
$count = 0;
# print " P: $filename\n";
# retrieve complete file
open (IN, "$filename") || die("Error Reading File: $filename $!");
{
undef $/;
$infile = <IN>;
while ($infile =~ m/$find/gi)
{$count++};
print $filename . " contains " . $find . " " . $count . (($count == 1) ? " time\n" : " times\n");
}
# print $filename . " contains " . $find . " " . $count . (($count == 1) ? " time\n" : " times\n");
close (IN) || die("Error Closing File: $filename $!");
}
print "\nFinished.\n";

exit(0);

PLEASE use code tags for code!

NO idea what you are talking about. You would have to give an example.

I also want to be able to include path of any file that contains what I am looking for. in my example I am looking for unixsip within files in all . (directories). Can anyone help.

I execute my code as :

perl alsfind.pl unixsip .

below is my code:

#!/usr/bin/perl

my $find = $ARGV[0];
my $replace = $ARGV[1];
my $glob = $ARGV[2];
@filelist = <*$glob>;
 
# process each file in file list
foreach $filename (@filelist) {
      $count = 0;
      # print "    P: $filename\n";
      # retrieve complete file
      open (IN, "$filename") || die("Error Reading File: $filename $!");
      {
        undef $/;          
        $infile = <IN>;
        while ($infile =~ m/$find/gi) 
            {$count++};
        print $filename . " contains " . $find . " " . $count . (($count == 1) ? " time\n" : " times\n");
       }
#     print $filename . " contains " . $find . " " . $count . (($count == 1) ? " time\n" : " times\n");
       close (IN) || die("Error Closing File: $filename $!");
}
print "\nFinished.\n";

Look at your opening post, where a moderator edited your post to put in code tags for you. And your latest post, where another moderator edited your post to put in code tags for you. And my original post, where I showed you how to do it!

Do a perldoc on File::Basename

perldoc File::Basename

I would also recommend including this in all your Perl code.

use strict;
use warnings;

As I mentioned I am learning on the fly at work with no help from anyone here. No one here knows it. This is a real script I am trying to create. I will no longer ask any help from this site.

Unix and perl are not within my job description but was assigned the task to do anyone.

Thanks for the help you all have provided.