From 4932ef0f92a12135fbbfe413c7e19e3e7636d002 Mon Sep 17 00:00:00 2001 From: Michael Mikonos <127171689+mknos@users.noreply.github.com> Date: Wed, 13 Nov 2024 19:52:22 +0800 Subject: [PATCH] grep: replace -1 with -m NUM (#809) * Previously this grep had a -1 flag meaning one match per file * The -1 flag is incompatible with GNU and BSD grep because they interpret -1 as -C 1, i.e. show one line of context around each match * Implement the common way to limit matches by adding -m (maximum) option * Allow -m0 because this is supported in the other versions * test1: perl grep -1 pat file1 file2 ---> no longer supported * test2: perl grep -m 2 pat file1 file2 ---> two matches per file --- bin/grep | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/bin/grep b/bin/grep index 8b07c13b..7baa4a6d 100755 --- a/bin/grep +++ b/bin/grep @@ -54,7 +54,7 @@ use File::Spec; use File::Temp qw(); use Getopt::Std; -our $VERSION = '1.010'; +our $VERSION = '1.011'; $| = 1; # autoflush output @@ -88,7 +88,7 @@ sub VERSION_MESSAGE { sub usage { die <) { + if (defined $opt->{'m'}) { # maximum may be zero + last LINE if $total >= $opt->{'m'}; + } $Matches = 0; ############## @@ -483,8 +488,6 @@ FILE: while ( defined( $file = shift(@_) ) ) { print $opt->{n} ? "$.:" : "", $_, ( $opt->{p} || $opt->{P} ) && ( '-' x 20 ) . "\n"; } - - last LINE if $opt->{'1'}; # single match per file } close $fh; } @@ -510,7 +513,8 @@ grep - search for regular expressions and print =head1 SYNOPSIS -B [ B<-[incCwsxvhHlLF1igurpaqT]> ] [ B<-e> I ] +B [ B<-[incCwsxvhHlLFigurpaqT]> ] [ B<-e> I ] [ B<-m> I ] + [ B<-f> I ] [ B<-P> I ] [ I ] [ I ... ] =head1 DESCRIPTION @@ -533,9 +537,9 @@ The following options are accepted: =over 4 -=item B<-1> +=item B<-m> I -Allow at most one match per file. +Display a maximum of NUM matches per file. =item B<-a>