Skip to content

Commit 0c8e2af

Browse files
authored
grep: filename prefix correction (#709)
* grep output is expected to show the filename if grep is searching many files, or in a recursive search (grep -r) * The -h flag disables the filename prefix * $Mult flag controls showing the filename, and was being set to 1 sometimes when it shouldn't be * test1: "perl grep perl awk" --> show matches for 1 file (no filename prefix, single file argument) * test2: "perl grep perl awk ar" --> matches for 2 files (filenames included) * test3: "perl grep -h perl awk ar" --> matches for 2 files (filenames disabled by -h) * test4: "perl grep -hr perl ." --> recursive search (filenames disabled) * test5: "echo elmo | perl grep elm" --> show matches for stdin (no filename prefix, no file arguments) * test6: "perl grep -l awk a*" --> show filenames matching awk
1 parent b0e27ac commit 0c8e2af

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

bin/grep

+6-4
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,6 @@ sub parse_args {
144144
@opt{ split //, $nulls } = ('') x length($nulls);
145145

146146
getopts('incCwsxvhe:f:l1HurtpP:aqTF', \%opt) or usage();
147-
$Mult = 1 if ($opt{'r'} || @ARGV > 1 || @ARGV > 0 && -d $ARGV[0]) && !$opt{'h'};
148147

149148
my $no_re = $opt{F} || ( $Me =~ /\bfgrep\b/ );
150149
$match_code = '';
@@ -174,6 +173,7 @@ sub parse_args {
174173
}
175174
@patterns = ($pattern);
176175
}
176+
$Mult = ($opt{'r'} || scalar(@ARGV) > 1) ^ $opt{'h'};
177177
@ARGV = ($opt{'r'} ? '.' : '-') unless @ARGV;
178178

179179
if ( $opt{H} || $opt{u} ) { # highlight or underline
@@ -391,9 +391,11 @@ FILE: while ( defined( $file = shift(@_) ) ) {
391391

392392
print("$name\n"), next FILE if $opt->{l};
393393
my $showmatch = !$opt->{'q'} && !$opt->{'c'};
394-
$showmatch && print $Mult && "$name:",
395-
$opt->{n} ? "$.:" : "", $_,
396-
( $opt->{p} || $opt->{P} ) && ( '-' x 20 ) . "\n";
394+
if ($showmatch) {
395+
print($name, ':') if $Mult;
396+
print $opt->{n} ? "$.:" : "", $_,
397+
( $opt->{p} || $opt->{P} ) && ( '-' x 20 ) . "\n";
398+
}
397399

398400
next FILE if (!$opt->{'c'} && $opt->{'1'} || $opt->{'q'}); # single match
399401
}

0 commit comments

Comments
 (0)