Perl - Count occurences


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl - Count occurences
# 15  
Old 01-30-2009
PLEASE use code tags for code!
# 16  
Old 01-30-2009
No idea

NO idea what you are talking about. You would have to give an example.
# 17  
Old 01-30-2009
Determining path

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:

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


Last edited by otheus; 01-30-2009 at 09:07 AM.. Reason: added code tags.
# 18  
Old 01-30-2009
Quote:
Originally Posted by TimHortons
NO idea what you are talking about. You would have to give an example.
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!
# 19  
Old 01-30-2009
Do a perldoc on File::Basename

Code:
perldoc File::Basename

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

Code:
use strict;
use warnings;

# 20  
Old 01-30-2009
No Problem

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.
# 21  
Old 01-30-2009
Here is how to get the path name:

Code:
$ cat ./pathtest.pl
#!/usr/bin/perl

use strict;
use warnings;
use File::Basename;

my $filename = "/usr/bin/perl";
my $dirname = dirname($filename);
my $basename = basename($filename);

print "The filename is $filename\n";
print "The path is $dirname\n";
print "The basename is $basename\n";
print "Put it together and they spell $dirname/$basename\n";

exit 0;

$ ./pathtest.pl
The filename is /usr/bin/perl
The path is /usr/bin
The basename is perl
Put it together and they spell /usr/bin/perl

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