Skip to content

Commit b6e4ec7

Browse files
authored
grep: allow repeated -e and -f (#757)
* If -e and -f are not provided, the first argument to grep is a pattern * If -e or -f is provided, the first argument to grep is a file * Allow multiple -e and -f options to translate to multiple elements in the patterns-list * This patch follows how GNU and BSD versions behave * Bump version * test1: "perl grep -e include -e return a.c" --> 2 patterns, one file argument * test2: "echo include > P1 && echo return > P2 && perl grep -f P1 -f P2 a.c" --> 2 pattern files with one pattern each; one file argument * test3: "grep return a.c" --> one (default) pattern argument,one file argument
1 parent c4ddf32 commit b6e4ec7

File tree

1 file changed

+38
-25
lines changed

1 file changed

+38
-25
lines changed

bin/grep

Lines changed: 38 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ use File::Spec;
5454
use File::Temp qw();
5555
use Getopt::Std;
5656

57-
our $VERSION = '1.006';
57+
our $VERSION = '1.007';
5858

5959
$| = 1; # autoflush output
6060

@@ -216,39 +216,52 @@ sub parse_args {
216216
unshift @ARGV, $_;
217217
}
218218

219+
# multiple -e/-f options
220+
my @tmparg;
221+
while (@ARGV) {
222+
my $arg = shift @ARGV;
223+
if ($arg eq '-e') {
224+
$pattern = shift @ARGV;
225+
usage() unless defined $pattern;
226+
push @patterns, $pattern;
227+
}
228+
elsif ($arg eq '-f') {
229+
my $file = shift @ARGV;
230+
usage() unless defined $file;
231+
die "$Me: $file: is a directory\n" if -d $file;
232+
my $fh;
233+
open($fh, '<', $file) or die "$Me: Can't open '$file': $!\n";
234+
my $line;
235+
while (defined($line = <$fh>)) {
236+
chomp $line;
237+
$pattern = $line;
238+
push @patterns, $pattern;
239+
}
240+
close $fh;
241+
242+
}
243+
else {
244+
push @tmparg, $arg;
245+
}
246+
}
247+
@ARGV = @tmparg;
248+
219249
$opt{'p'} = $opt{'P'} = ''; # argument to print()
220250
getopts('inCcwsxvHhe:f:Ll1gurpP:aqTF', \%opt) or usage();
221251

222252
$opt{'l'} = 0 if $opt{'L'};
223253
my $no_re = $opt{F} || ( $Me =~ /\bfgrep\b/ );
224254

225-
if (defined $opt{'f'}) { # -f patfile
226-
my $path = $opt{'f'};
227-
my $patfile;
228-
die "$Me: $path: is a directory\n" if -d $path;
229-
open($patfile, '<', $path) or die "$Me: Can't open '$path': $!\n";
230-
231-
# make sure each pattern in file is valid
232-
while ( defined( $pattern = <$patfile> ) ) {
233-
chomp $pattern;
234-
unless ($no_re) {
235-
eval { 'foo' =~ /$pattern/, 1 }
236-
or die "$Me: $path: $.: bad pattern: $@\n";
237-
}
238-
push @patterns, $pattern;
239-
}
240-
close $patfile;
241-
}
242-
else { # make sure pattern is valid
243-
$pattern = $opt{'e'};
244-
$pattern = shift(@ARGV) unless length $pattern;
245-
usage() unless defined $pattern;
246-
unless ($no_re) {
255+
unless (length $pattern) {
256+
$pattern = shift @ARGV;
257+
push @patterns, $pattern;
258+
}
259+
unless ($no_re) {
260+
foreach $pattern (@patterns) {
247261
eval { 'foo' =~ /$pattern/, 1 }
248262
or die "$Me: bad pattern: $@\n";
249-
}
250-
@patterns = ($pattern);
251263
}
264+
}
252265
if ($opt{'H'}) {
253266
$Mult = 1;
254267
}

0 commit comments

Comments
 (0)