Perl: Counting matches


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl: Counting matches
# 1  
Old 04-30-2012
Perl: Counting matches

Hi,
A perl newbie here so pretty sure it's something simple. Trying to figure out how to count matches with perl pattern matching. The following script opens a text data file and finds lines containing
"PORT:" and I'd like to count how many of these are found.

Any ideas?

Code:
open(LOG,"< test_small.xml") or die "Unable to open logfile:$!\n";

while(<LOG>) {

        my $line = $_;

        if ($line =~ m/^> PORTZONESHOW/.../^> */) {
                if ( $line =~ m/^PORT:/) {
                        print $line;
                }
        }
}

Output:
Code:
PORT: 0 (0)   Offline
PORT: 1 (1)   Offline
PORT: 2 (2)   Offline
PORT: 3 (3)   Offline
PORT: 4 (4)   Offline
PORT: 5 (5)   Offline
PORT: 6 (6)   F-Port    Enforcement: HARD PORT  defaultHard: 1  IFID: 0x43020012
PORT: 7 (7)   F-Port    Enforcement: HARD WWN   defaultHard: 0  IFID: 0x43020014
PORT: 8 (8)   F-Port    Enforcement: HARD WWN   defaultHard: 0  IFID: 0x43020016
PORT: 9 (9)   Offline
PORT: 10 (10)   F-Port  Enforcement: HARD PORT  defaultHard: 1  IFID: 0x4302000c
PORT: 11 (11)   F-Port  Enforcement: HARD WWN   defaultHard: 0  IFID: 0x4302000d
PORT: 12 (12)   F-Port  Enforcement: HARD WWN   defaultHard: 0  IFID: 0x4302000f
PORT: 13 (13)   Offline
PORT: 14 (14)   F-Port  Enforcement: HARD WWN   defaultHard: 0  IFID: 0x43020013
PORT: 15 (15)   F-Port  Enforcement: HARD WWN   defaultHard: 0  IFID: 0x43020015
PORT: 16 (16)   Offline
PORT: 17 (17)   E-Port
PORT: 18 (18)   Offline
PORT: 19 (19)   Offline
PORT: 20 (20)   Offline
PORT: 21 (21)   Offline
PORT: 22 (22)   Offline
PORT: 23 (23)   Offline


Last edited by hdefjunkie; 04-30-2012 at 12:42 AM..
# 2  
Old 04-30-2012
Code:
open(LOG,"< test_small.xml") or die "Unable to open logfile:$!\n";

my $count = 0; # Initialise a count variable to 0.
while(<LOG>) {
    my $line = $_;
    if ($line =~ m/^> PORTZONESHOW/../^> */) { # Did you mean 2 dots here instead of 3?
        if ( $line =~ m/^PORT:/) {
            $count++; # Increment it everytime you find a match.
            print $line;
        }
    }
}

close LOG; # Its a good practice to close file handles yourself.
print "Count: $count\n"; # Print $count

This User Gave Thanks to balajesuri For This Post:
# 3  
Old 04-30-2012
Thx... Brain fart :-)
# 4  
Old 05-03-2012
As I see you already have the answer. Let me point out a few ways you could improve your Perl code.
1)
Code:
use strict; use warnings;

These usually help avoiding some common bugs. (Not this case but in general)
2) open is better written with 3 parameters as in this code:
Code:
open(my $LOG,"<", "test_small.xml") or die ...

3) You don't need to use $_ in the while loop
Code:
while(my $line = <$LOG>) {


Last edited by Scrutinizer; 05-03-2012 at 03:48 AM.. Reason: code tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Word counting perl script

Hi friends i need a help on Perl Script In My Home directory, i have some other directories and inside those directories i have some subdirectories and all the directories contains files. Now i want to count a word in all files and i want the output like below wordcount in which file(name... (5 Replies)
Discussion started by: siva kumar
5 Replies

2. Shell Programming and Scripting

Compare 2 files and print matches and non-matches in separate files

Hi all, I have two files, chap.txt and complex.txt. chap.txt looks like this: a d l m r k complex.txt looks like this: a c d e l m n j a d l p q r c p r m ......... (7 Replies)
Discussion started by: AshwaniSharma09
7 Replies

3. UNIX for Dummies Questions & Answers

Print only matches

Im not sure how to tell this awk command to only print when there is a match between the two files. Right now its printing the words in quotation even if there is not a match. I tried removing the additional info and it prints blank spaces?? awk ' { IP = $1 $1 = "" } FNR == NR { ... (2 Replies)
Discussion started by: sectech
2 Replies

4. Shell Programming and Scripting

Displaying the first field if the second field matches the pattern using Perl

Hi, I am trying with the below Perl command to print the first field when the second field matches the given pattern: perl -lane 'open F, "< myfile"; for $i (<F>) {chomp $i; if ($F =~ /patt$/) {my $f = (split(" ", $i)); print "$f";}} close F' dummy_file I know I can achieve the same with the... (7 Replies)
Discussion started by: royalibrahim
7 Replies

5. UNIX for Dummies Questions & Answers

Counting feilds entries with Perl

Hi All, I have a small problem of counting the number of times a particular entry that exists in a horizontal string of elements and a vertical feild (column of entries). For example AATGGTCCTGExpected outputA=2 C=2 G=3 T=3 I have an idea to do this but I dont know how to do that if these entries... (1 Reply)
Discussion started by: pawannoel
1 Replies

6. Shell Programming and Scripting

counting words with perl?

how to use perl to count number of lines, words characters in a file. (3 Replies)
Discussion started by: winter9
3 Replies

7. Shell Programming and Scripting

Printing between 2 matches with Perl

Can we please modify this perl one-liner to print lines between pattern1 and pattern2 in a file? Currently it prints lines till pattern2. (4 Replies)
Discussion started by: anand_bh
4 Replies

8. Shell Programming and Scripting

Perl line count if it matches a pattern

#!/usr/bin/perl use Shell; open THEFILE, "C:\galileo_integration.txt" || die "Couldnt open the file!"; @wholeThing = <THEFILE>; close THEFILE; foreach $line (@wholeThing){ if ($line =~ m/\\0$/){ @nextThing = $line; if ($line =~ s/\\0/\\LATEST/g){ @otherThing =... (2 Replies)
Discussion started by: nmattam
2 Replies

9. Shell Programming and Scripting

Matches and mismatches in perl

When we give an input sequence , the program should match with the pattern and give the matches and mismatches in the output. i will give you 2 small examples. if you cant get it pls let me know. i will try to give a clear idea. example 1: $a=APPLE; # let it be a pattern... (0 Replies)
Discussion started by: srisha
0 Replies

10. Shell Programming and Scripting

how to get the pos() of 2 str matches in one loop in perl

Hello all i have this simple loop that gets me every time the match of "<#" in my string something like that : my $str ="gggg<#nnnnn#>kkkk<#ssss#>llllll"; while($str =~m/<#/g){ print pos($str); } but now i like to get another pos in the same loop iteration , i will like to get the... (1 Reply)
Discussion started by: umen
1 Replies
Login or Register to Ask a Question