Solved

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

Posted on 2012-04-03
3
298 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 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

We have adopted the strategy to use Computers in Student Labs as the bulletin boards. The same target can be achieved by using a Login Notice feature in Group policy but it’s not as attractive as graphical wallpapers with message which grabs the att…
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…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

740 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