?
Solved

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

Posted on 2012-04-03
3
Medium Priority
?
300 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 31

Assisted Solution

by:farzanj
farzanj earned 400 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 1600 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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Sometimes people don't understand why download speed shows differently for Windows than Linux.Specially, this article covers and shows the solution for throughput difference for Windows than a Linux machine. For this, I arranged a test scenario.I…
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…
Six Sigma Control Plans
Suggested Courses

777 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