Re: PERL pattern matching (100 Views)
Reply
Occasional Contributor
Pat Tom
Posts: 3
Registered: ‎01-18-2007
Message 1 of 4 (100 Views)

PERL pattern matching

I have a flat file which contains hundreds of patterns. I need to write a PERL script that parses this file and returns each unique pattern and the count of its occurance.

I am trying it for quite sometime but not able to code it.

Can anyone please help.

Thanks
Pat
Please use plain text.
Honored Contributor
H.Merijn Brand (procura
Posts: 6,185
Registered: ‎10-13-1997
Message 2 of 4 (100 Views)

Re: PERL pattern matching

#!/opt/perl/bin/perl

use strict;
use warnings;

my %pat;
while (<>) {
$pat{$_}++;
}
foreach my $pat (sort keys %pat) {
printf "%6d %s", $pat{$pat}, $pat;
}

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 24,953
Registered: ‎03-06-2006
Message 3 of 4 (100 Views)

Re: PERL pattern matching

What do you mean by patterns? A file can have tokens or words but RE patterns is something only a human can find. After all, one pattern can be made for all English words.

If you want to search for space delimited alphabetic tokens you can use:
$ tr -cs "[A-Z][a-z]" "[\012*]" < file | sort | uniq -c
Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 4 of 4 (100 Views)

Re: PERL pattern matching

Hi Pat:

Merijn's script gives you the solution based on the contents of a *line*. If your lines contain multiple "words" this variation gives you their unique counts.

#!/usr/bin/perl
use strict;
use warnings;

my %pat;
while (<>) {
my @a=m{\w+}g;
$pat{$_}++ for (@a);
}
foreach my $pat (sort keys %pat) {
printf "%6d %s\n", $pat{$pat}, $pat;
}

The \w regular expression matches an alphanumeric character or underscore but not a hyphen, quote, comma, semicolon, colons, etc.

As for writing the word "PERL" -- don't -- we are speaking of the "Perl" language:

http://www.perl.org/about/style-guide.html

You have previously posted another question about pattern matching for which solutions were provided. You forgot to score those solutions. It would be appreciated:

http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=1100015

Regards!

...JRF...
Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation