PERL pattern matching (129 Views)
Reply
Highlighted
Occasional Contributor
Posts: 3
Registered: ‎01-18-2007
Message 1 of 4 (129 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
Honored Contributor
Posts: 6,195
Registered: ‎10-13-1997
Message 2 of 4 (129 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
Acclaimed Contributor
Posts: 25,902
Registered: ‎03-06-2006
Message 3 of 4 (129 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
Acclaimed Contributor
Posts: 21,184
Registered: ‎07-06-2000
Message 4 of 4 (129 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...
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.