From 6b803bbb4c0f4fd445d5cfd4b2187bfdd09ea2c6 Mon Sep 17 00:00:00 2001 From: Michael Mikonos <127171689+mknos@users.noreply.github.com> Date: Sun, 17 Nov 2024 08:07:02 +0800 Subject: [PATCH] grep: add -I flag * BSD and GNU versions have an -I option for ignoring binary files, i.e. treating binary files as if they don't match * test1: perl grep BASH /bin/bash ---> match, suppress text * test2: perl grep -a BASH /bin/bash ---> match, show text * test3: perl grep -q BASH /bin/bash ---> match, suppress all output * test4: perl grep -I BASH /bin/bash text.txt ---> match on text.txt but not /bin/bash --- bin/grep | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/bin/grep b/bin/grep index 1a74f385..268bd474 100755 --- a/bin/grep +++ b/bin/grep @@ -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 @@ -88,7 +88,7 @@ sub VERSION_MESSAGE { sub usage { die <{'T'}; @@ -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; @@ -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 @@ -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