Dotfiles, utilities, and other apparatus.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

23 lines
417 B

#!/usr/bin/env perl
# matches-per-line - prefix lines with count of matches for given regex
#
# Usage:
#
# echo 'RRR' | matches-per-line "R"
# matches-per-line R ./example.txt
use warnings;
use strict;
my $pattern = shift @ARGV;
while (my $line = <>) {
chomp $line;
# Get a count of matching substrings:
my @matches = $line =~ m/$pattern/g;
my $count = scalar @matches;
print "$count\t$line\n";
}