Skip to content

Commit

Permalink
grep: add -L option (#745)
Browse files Browse the repository at this point in the history
* GNU and BSD versions support -L which is the opposite of -l
* Using -L is easier than filtering grep -c output for 0
* Document that -L overrides -l and update usage string
* Bump version

%perl grep -c include ar a.c
ar:0
a.c:2
%perl grep -l include ar a.c
a.c
%perl grep -L include ar a.c
ar
  • Loading branch information
mknos authored Sep 26, 2024
1 parent c84e378 commit e4c9d76
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions bin/grep
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ use File::Basename qw(basename);
use File::Spec;
use Getopt::Std;

our $VERSION = '1.004';
our $VERSION = '1.005';

$| = 1; # autoflush output

Expand Down Expand Up @@ -87,7 +87,7 @@ sub VERSION_MESSAGE {

sub usage {
die <<EOF;
usage: $Me [-incCwsxvHhlF1gurtpaqT] [-e pattern]
usage: $Me [-inCcwsxvHhLlF1gurtpaqT] [-e pattern]
[-f pattern-file] [-P sep] [pattern] [file...]
Options:
Expand All @@ -104,6 +104,7 @@ Options:
-e expression (for exprs beginning with -)
-f file with expressions
-l list filenames matching
-L list filenames which do not match
-F search for fixed strings (disable regular expressions)
-1 1 match per file
-g highlight matches
Expand Down Expand Up @@ -131,14 +132,15 @@ sub parse_args {
unshift @ARGV, $_;
}

$zeros = 'inCwxvghHlut'; # options to init to 0 (prevent warnings)
$zeros = 'inCwxvghHLlut'; # options to init to 0 (prevent warnings)
$nulls = 'epP'; # options to init to "" (prevent warnings)

@opt{ split //, $zeros } = (0) x length($zeros);
@opt{ split //, $nulls } = ('') x length($nulls);

getopts('incCwsxvHhe:f:l1gurtpP:aqTF', \%opt) or usage();
getopts('inCcwsxvHhe:f:Ll1gurtpP:aqTF', \%opt) or usage();

$opt->{'l'} = 0 if $opt->{'L'};
my $no_re = $opt{F} || ( $Me =~ /\bfgrep\b/ );
$match_code = '';

Expand Down Expand Up @@ -392,6 +394,7 @@ FILE: while ( defined( $file = shift(@_) ) ) {

$total += $Matches;
last FILE if $opt->{'q'}; # single match for all files
last LINE if $opt->{'L'}; # one match is enough

if ( $opt->{p} || $opt->{P} ) {
s/\n{2,}$/\n/ if $opt->{p};
Expand All @@ -415,6 +418,9 @@ FILE: while ( defined( $file = shift(@_) ) ) {
print($name, ':') if $Mult;
print $total, "\n";
}
if ($opt->{'L'} && !$total) {
print $name, "\n";
}
close FILE;
}
$Grand_Total += $total;
Expand All @@ -430,7 +436,7 @@ grep - search for regular expressions and print
=head1 SYNOPSIS
B<grep> [ B<-[incCwsxvhHlF1igurtpaqT]> ] [ B<-e> I<pattern> ]
B<grep> [ B<-[incCwsxvhHlLF1igurtpaqT]> ] [ B<-e> I<pattern> ]
[ B<-f> I<pattern-file> ] [ B<-P> I<sep> ] [ I<pattern> ] [ I<files> ... ]
=head1 DESCRIPTION
Expand Down Expand Up @@ -516,6 +522,11 @@ C<unix> would match C<unix> as well as C<UniX> (plus the other fourteen
possible capitalizations). This corresponds to the C</i> Perl regular
expression switch. See L<perlre>.
=item B<-L>
List files which do no match any pattern. This option takes precedence
over B<-l>.
=item B<-l>
List files containing matches. This option tells B<grep> not to
Expand Down

0 comments on commit e4c9d76

Please sign in to comment.