Solved

Request a perl one-liner to count matched patterns in a file

Posted on 2012-04-03
3
287 Views
Last Modified: 2012-04-03
Request a perl to count matched patterns in a file. Is one-liner possible? The environment is ms windows. For example,
in test.txt,

aa bb 12 32 24 bc d1 ff f0 cc
24 bc d1 99 0f cd ab db ea 12
12 23 34 45 54 55 21 78 34 89
cd ae de ef ca cb da 24 bc d1
cd 24 bc d1 24 bc d1 ca cb da
.....

Want to count how many pattern "24 bc d1" in this file. Not need to consider the overlapped case such as the line 12 12 12 12 if the matched pattern is "12 12 12".
0
Comment
Question by:jl66
3 Comments
 
LVL 31

Assisted Solution

by:farzanj
farzanj earned 100 total points
ID: 37801209
Something like

my $pattern = '24 bc d1';
$/ = undef;
my $text = <>;
my (@words) = $text =~ /$pattern/g;
print @words . "\n";
0
 
LVL 84

Accepted Solution

by:
ozo earned 400 total points
ID: 37801488
perl -ne "BEGIN{$/='24 bc d1'}$c=chomp;END{print $.-!$c}" test.txt
0
 

Author Closing Comment

by:jl66
ID: 37801925
Thanks a lot for the solution.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

It is only natural that we all want our PCs to be in good working order, improved system performance, so that is exactly how programs are advertised to entice. They say things like:            •      PC crashes? Get registry cleaner to repair it!    …
Issue: Unstable cursor in Windows XP and Windows runs extremely slow in that any click will bring up the Hour glass (sometimes for several seconds before giving you what you want) . Troubleshooting Process and the FINAL FIX: This issue see…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now