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

  1. #!/usr/bin/env perl
  2. # matches-per-line - prefix lines with count of matches for given regex
  3. #
  4. # Usage:
  5. #
  6. # echo 'RRR' | matches-per-line "R"
  7. # matches-per-line R ./example.txt
  8. use warnings;
  9. use strict;
  10. my $pattern = shift @ARGV;
  11. while (my $line = <>) {
  12. chomp $line;
  13. # Get a count of matching substrings:
  14. my @matches = $line =~ m/$pattern/g;
  15. my $count = scalar @matches;
  16. print "$count\t$line\n";
  17. }