Perl - Count occurences


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl - Count occurences
# 8  
Old 01-29-2009
perl can use system commands, yes. if that's all your perl script needs to do though, why use perl at all?
# 9  
Old 01-29-2009
why Perl

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.
# 10  
Old 01-29-2009
Perl grep

What is the syntax for Grep in Perl.. It is a built in caommand and different from unixs syntax
# 11  
Old 01-29-2009
Type in

Code:
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
# 12  
Old 01-29-2009
I think he means:
Code:
$/usr/bin/perl
print `/usr/bin/grep -c "searchstring"  @ARGV`

# 13  
Old 01-29-2009
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:

Code:
    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.
# 14  
Old 01-29-2009
My solution

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);
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