Perl - Count occurences


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl - Count occurences
# 1  
Old 01-29-2009
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:
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>;
   }
    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);


Last edited by jim mcnamara; 01-29-2009 at 02:00 PM.. Reason: edited - still has syntax errors
# 2  
Old 01-29-2009
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.
# 3  
Old 01-29-2009
new to perl

I am new to perl and do not understand your answer. Can you tell me specifically the error in my code?
# 4  
Old 01-29-2009
I added code tags to your code. I think there is a { .. } mismatch in your example.
# 5  
Old 01-29-2009
I found a couple of logic flaws but still no output of counts

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);
# 6  
Old 01-29-2009
I'm not being funny is there a requirement not to do this:
Code:
grep -c 'search string' file file1 file2

# 7  
Old 01-29-2009
DUmmyme

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

Thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Count occurences of the word without it repeating

Hi, I would like to count the number of ALA occurences without having them to be repeated. In the script I have written now it has 40 repetitions of ALA but it has to be 8. ALA is chosen as one of the 20 values it can have when the script asks for the input of AAA, which for this example is chosen... (7 Replies)
Discussion started by: Aurimas
7 Replies

2. Shell Programming and Scripting

Count the occurences of strings

I have some text files in a folder f1 with 10 columns. The first five columns of a file are shown below. aab abb 263-455 263 455 aab abb 263-455 263 455 aab abb 263-455 263 455 bbb abb 26-455 26 455 bbb abb 26-455 26 455 bbb aka 264-266 264 266 bga bga 230-232 230 ... (10 Replies)
Discussion started by: gomez
10 Replies

3. Shell Programming and Scripting

awk count occurences

line number:status, market, keystation 1,SENT,EBS,1 : 1 2,DONE,REU,1 : 1 3,SENT,EBS,2 : 1 4,DONE,EBS,1 : 0 5,SENT,EBS,2 : 0 6,SENT,EBS,2 : 0 7,SENT,EBS,2 : 0 8,SENT,EBS,1 : 1 for each status, market combination I want to keep a tally of active orders. i.e if an order is SENT, then +1, if... (8 Replies)
Discussion started by: Calypso
8 Replies

4. Shell Programming and Scripting

Count number of occurences using awk

Hi Guys, I have 2 files like below file1 xx yy file2 b yy b2 xx c1 yy xx yy Now I want an idea which can count occurences of text from file1 and file2 so outbout would be kind of (9 Replies)
Discussion started by: prashant2507198
9 Replies

5. Shell Programming and Scripting

Count occurences based on interval

Hi, I have a file which has 4500 entries 10000 9880 9800 8700 8200 ... ..... ... ... ... ... ... ... ... 50 (1 Reply)
Discussion started by: Diya123
1 Replies

6. UNIX for Dummies Questions & Answers

Count pattern occurences

hi, I have a text..and i need to find a pattern in the text and count to the no of times the pattern occured. i have used grep command ..but the problem is , it shows the occurrences of the pattern but doesn't count no of times the pattern occuries. (5 Replies)
Discussion started by: nvnni
5 Replies

7. Shell Programming and Scripting

Count occurences of string

Hi, Please help me in finding the number of occurences of the string. Example: Apple, green, blue, Apple, Orange, green, blue are the strings can be even in the next line. The o/p should look as: Word Count ----- ----- Apple 2 green 2 Orange 1 blue 2 Thanks (2 Replies)
Discussion started by: acc888
2 Replies

8. Shell Programming and Scripting

Awk to count occurences

Hi, i am in need of an awk script to accomplish the following: Input table looks like: Student1 arts Student2 science Student3 arts Student4 science Student5 science Student6 science Student7 science Student8 science Student9 science Student10 science Student11 science... (8 Replies)
Discussion started by: saint2006
8 Replies

9. UNIX for Dummies Questions & Answers

Count number of occurences of a word

I want to count the number of occurences of say "200" in a file but that file also contains various stuff including dtaes like 2007 or smtg like 200.1 so count i am getting by doing grep -c "word" file is wrong Please help!!!!! (8 Replies)
Discussion started by: shikhakaul
8 Replies

10. Shell Programming and Scripting

How to count the number of occurences of this pattern?

Hi all, I have a pattern like this in a file: 123 4 56 789 234 5 67 789 121 3 56 789 222 4 65 789 321 6 90 100 478 8 40 789 243 7 80 789 How can I count the number of occurences of '789' (4th column) in this set...? Thanks for all your help! K (7 Replies)
Discussion started by: kripssmart
7 Replies
Login or Register to Ask a Question