Skip to content

Commit b0e27ac

Browse files
authored
grep: -q and -s options reversed (#708)
* This grep has the standard meanings of options -s and -q backwards [1] * -s is to suppress some errors * -q is to make grep not show matches, and perform a quick search of one match per file * test1: "perl grep -c yo grep" --> count the matches of "yo" * test2: "perl grep -cv yo grep"--> count the lines not matching "yo" * test3: "perl grep -q perl grep" --> quiet search for "perl"; exit status 0 indicates Match * test4: "perl grep -q pascal grep" --> quiet search for "pascal"; exit status 1 indicates NoMatch * test4: "perl grep -s greg greg" --> silent search for "greg" but the file doesn't exist; exit status 2 indicates Error * Correct POD manual and usage string 1. https://pubs.opengroup.org/onlinepubs/009604299/utilities/grep.html
1 parent c3afc01 commit b0e27ac

File tree

1 file changed

+15
-20
lines changed

1 file changed

+15
-20
lines changed

bin/grep

+15-20
Original file line numberDiff line numberDiff line change
@@ -97,22 +97,20 @@ sub usage {
9797
usage: $Me [-incCwsxvhlF1HurtpaqT] [-e pattern]
9898
[-f pattern-file] [-P sep] [pattern] [file...]
9999
100-
Standard grep options:
100+
Options:
101101
-i case insensitive
102102
-n number lines
103103
-c give count of lines matching
104104
-C ditto, but >1 match per line possible
105105
-w word boundaries only
106-
-s silent mode
106+
-q quiet; nothing is written to standard output
107107
-x exact matches only
108108
-v invert search sense (lines that DON'T match)
109109
-h hide filenames
110110
-e expression (for exprs beginning with -)
111111
-f file with expressions
112112
-l list filenames matching
113113
-F search for fixed strings (disable regular expressions)
114-
115-
Specials:
116114
-1 1 match per file
117115
-H highlight matches
118116
-u underline matches
@@ -121,7 +119,7 @@ Specials:
121119
-p paragraph mode (default: line mode)
122120
-P ditto, but specify separator, e.g. -P '%%\\n'
123121
-a all files, not just plain text files
124-
-q quiet about failed file and dir opens
122+
-s suppress errors for failed file and dir opens
125123
-T trace files as opened
126124
127125
May use GREP_OPTIONS environment variable to set default options.
@@ -146,7 +144,6 @@ sub parse_args {
146144
@opt{ split //, $nulls } = ('') x length($nulls);
147145

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

152149
my $no_re = $opt{F} || ( $Me =~ /\bfgrep\b/ );
@@ -244,7 +241,6 @@ sub parse_args {
244241
$opt{1} += $opt{l}; # that's a one and an ell
245242
$opt{H} += $opt{u};
246243
$opt{c} += $opt{C};
247-
$opt{1} += $opt{'s'} && !$opt{c}; # that's a one
248244

249245
$match_code .= 'study;' if @patterns > 5; # might speed things up a bit
250246

@@ -291,7 +287,7 @@ FILE: while ( defined( $file = shift(@_) ) ) {
291287
my $compressed = 0;
292288

293289
if ( $file eq '-' ) {
294-
warn "$Me: reading from stdin\n" if -t STDIN && !$opt->{'q'};
290+
warn "$Me: reading from stdin\n" if -t STDIN && !$opt->{'s'};
295291
$name = '<STDIN>';
296292
}
297293
elsif ( -d $file ) {
@@ -306,7 +302,7 @@ FILE: while ( defined( $file = shift(@_) ) ) {
306302
next FILE;
307303
}
308304
unless ( opendir( DIR, $file ) ) {
309-
unless ( $opt->{'q'} ) {
305+
unless ( $opt->{'s'} ) {
310306
warn "$Me: can't opendir $file: $!\n";
311307
$Errors++;
312308
}
@@ -332,7 +328,7 @@ FILE: while ( defined( $file = shift(@_) ) ) {
332328
else {
333329
$name = $file;
334330
unless ( -e $file ) {
335-
warn qq($Me: "$file" does not exist\n) unless $opt->{'q'};
331+
warn qq($Me: "$file" does not exist\n) unless $opt->{'s'};
336332
$Errors++;
337333
next FILE;
338334
}
@@ -366,7 +362,7 @@ FILE: while ( defined( $file = shift(@_) ) ) {
366362
else {
367363
$ok = open $fh, '<', $file;
368364
}
369-
if ( !$ok && !$opt->{'q'} ) {
365+
if ( !$ok && !$opt->{'s'} ) {
370366
warn "$Me: $file: $!\n";
371367
$Errors++;
372368
}
@@ -394,12 +390,12 @@ FILE: while ( defined( $file = shift(@_) ) ) {
394390
}
395391

396392
print("$name\n"), next FILE if $opt->{l};
397-
398-
$opt->{'s'} || print $Mult && "$name:",
393+
my $showmatch = !$opt->{'q'} && !$opt->{'c'};
394+
$showmatch && print $Mult && "$name:",
399395
$opt->{n} ? "$.:" : "", $_,
400396
( $opt->{p} || $opt->{P} ) && ( '-' x 20 ) . "\n";
401397

402-
next FILE if $opt->{1}; # that's a one
398+
next FILE if (!$opt->{'c'} && $opt->{'1'} || $opt->{'q'}); # single match
403399
}
404400
}
405401
continue {
@@ -524,10 +520,9 @@ $/ scalar. See L<perlvar>.
524520
Paragraph mode. This causes B<grep> to set Perl's magic $/ to C<''>.
525521
(Note that the default is to process files in line mode.) See L<perlvar>.
526522
527-
=item B<-q>
523+
=item B<-s>
528524
529-
Quiet mode. Suppress diagnostic messages to the standard error. See
530-
B<-s>.
525+
Suppress diagnostic messages to the standard error.
531526
532527
=item B<-r>
533528
@@ -538,11 +533,11 @@ presence of B<-r> implies a file argument of F<.>, the current
538533
directory, if the user does not provide filenames on the command line.
539534
See L<"EXAMPLES">.
540535
541-
=item B<-s>
536+
=item B<-q>
542537
543-
Silent mode. Do not write to the standard output. This option would
538+
Quiet mode. Do not write to the standard output. This option would
544539
be useful from a shell script, for example, if you are only interested
545-
in whether or not there exists a match for a pattern. See also B<-q>.
540+
in whether or not there exists a match for a pattern.
546541
547542
=item B<-T>
548543

0 commit comments

Comments
 (0)