#!/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"; }