Sponsored Content
Full Discussion: Perl: Counting matches
Top Forums Shell Programming and Scripting Perl: Counting matches Post 302632343 by hdefjunkie on Sunday 29th of April 2012 11:34:25 PM
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..
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
GLOB_MATCH(9)						       libata Core Internals						     GLOB_MATCH(9)

NAME
glob_match - match a text string against a glob-style pattern SYNOPSIS
int glob_match(const char * text, const char * pattern); ARGUMENTS
text the string to be examined pattern the glob-style pattern to be matched against DESCRIPTION
Either/both of text and pattern can be empty strings. Match text against a glob-style pattern, with wildcards and simple sets: ? matches any single character. * matches any run of characters. [xyz] matches a single character from the set: x, y, or z. [a-d] matches a single character from the range: a, b, c, or d. [a-d0-9] matches a single character from either range. The special characters ?, [, -, or *, can be matched using a set, eg. [*] Behaviour with malformed patterns is undefined, though generally reasonable. SAMPLE PATTERNS
"SD1?", "SD1[0-5]", "*R0", "SD*1?[012]*xx" This function uses one level of recursion per '*' in pattern. Since it calls _nothing_ else, and has _no_ explicit local variables, this will not cause stack problems for any reasonable use here. RETURNS
0 on match, 1 otherwise. AUTHOR
Jeff Garzik Author. COPYRIGHT
Kernel Hackers Manual 3.10 June 2014 GLOB_MATCH(9)
All times are GMT -4. The time now is 06:57 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy