Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

grep: add -I flag #815

Merged
merged 1 commit into from
Nov 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions bin/grep
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ use File::Spec;
use File::Temp qw();
use Getopt::Std;

our $VERSION = '1.013';
our $VERSION = '1.014';

$| = 1; # autoflush output

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

sub usage {
die <<EOF;
usage: $Me [-incwsxvHhLlFgurpaqT] [-e pattern] [-A NUM] [-B NUM] [-C NUM]
usage: $Me [-IincwsxvHhLlFgurpaqT] [-e pattern] [-A NUM] [-B NUM] [-C NUM]
[-m NUM] [-f pattern-file] [-P sep] [pattern] [file...]

Options:
Expand Down Expand Up @@ -116,6 +116,7 @@ Options:
-B show lines before each matching line
-A show lines after each matching line
-a treat binary files as plain text files
-I ignore binary files
-s suppress errors for failed file and dir opens
-T trace files as opened
-Z force grep to behave as zgrep
Expand Down Expand Up @@ -245,7 +246,7 @@ sub parse_args {
@ARGV = @tmparg;

$opt{'p'} = $opt{'P'} = ''; # argument to print()
getopts('inC:cwsxvHhe:f:LlgurpP:aqTFZm:A:B:', \%opt) or usage();
getopts('IinC:cwsxvHhe:f:LlgurpP:aqTFZm:A:B:', \%opt) or usage();

if (defined $opt{'m'} && $opt{'m'} !~ m/\A[0-9]+\z/) {
die "$Me: invalid max count\n";
Expand Down Expand Up @@ -429,9 +430,7 @@ FILE: while ( defined( $file = shift(@_) ) ) {
$file = "$Compress{$ext} $file |";
$compressed = 1;
}
elsif (-B $file) {
$is_binary = 1;
}
$is_binary = 1 if -B $file;
}

warn "$Me: checking $name\n" if $opt->{'T'};
Expand All @@ -453,7 +452,10 @@ FILE: while ( defined( $file = shift(@_) ) ) {
$Errors++;
next FILE;
}
binmode($fh) if $is_binary;
if ($is_binary) {
next FILE if $opt->{'I'};
binmode $fh;
}
}

$total = $Matches = 0;
Expand Down Expand Up @@ -558,7 +560,7 @@ grep - search for regular expressions and print

=head1 SYNOPSIS

grep [-incwsxvhHlLFigurpaqT] [-e pattern] [-A NUM] [-B NUM] [-C NUM]
grep [-IincwsxvhHlLFigurpaqT] [-e pattern] [-A NUM] [-B NUM] [-C NUM]
[-m NUM] [-f pattern-file] [-P sep] [pattern] [file ...]

=head1 DESCRIPTION
Expand Down Expand Up @@ -642,6 +644,10 @@ Always include filename headers for matching lines or paragraphs.

Hide filenames. Only print matching lines or paragraphs.

=item B<-I>

Ignore binary files.

=item B<-i>

Ignore case while matching. This means, for example, that the pattern
Expand Down
Loading