GNU bug report logs - #45432
Use both --include and --exclude at the same time

Previous Next

Package: grep;

Reported by: "Fred .Flintstone" <eldmannen <at> gmail.com>

Date: Fri, 25 Dec 2020 16:57:02 UTC

Severity: normal

Done: Paul Eggert <eggert <at> cs.ucla.edu>

Bug is archived. No further changes may be made.

To add a comment to this bug, you must first unarchive it, by sending
a message to control AT debbugs.gnu.org, with unarchive 45432 in the body.
You can then email your comments to 45432 AT debbugs.gnu.org in the normal way.

Toggle the display of automated, internal messages from the tracker.

View this report as an mbox folder, status mbox, maintainer mbox


Report forwarded to bug-grep <at> gnu.org:
bug#45432; Package grep. (Fri, 25 Dec 2020 16:57:02 GMT) Full text and rfc822 format available.

Acknowledgement sent to "Fred .Flintstone" <eldmannen <at> gmail.com>:
New bug report received and forwarded. Copy sent to bug-grep <at> gnu.org. (Fri, 25 Dec 2020 16:57:02 GMT) Full text and rfc822 format available.

Message #5 received at submit <at> debbugs.gnu.org (full text, mbox):

From: "Fred .Flintstone" <eldmannen <at> gmail.com>
To: bug-grep <at> gnu.org
Subject: Use both --include and --exclude at the same time
Date: Fri, 25 Dec 2020 14:17:32 +0100
[Message part 1 (text/plain, inline)]
Hi,

It seems --exclude does nothing when --include is used. It would be useful
to be able to use both together, in order to do things such as recusively
grepping files of a certain file extension while excluding certain
directories.

Example:
$ grep --recursive --include="*.cs" --exclude="*/tests/*"
[Message part 2 (text/html, inline)]

Information forwarded to bug-grep <at> gnu.org:
bug#45432; Package grep. (Mon, 04 Jan 2021 17:57:01 GMT) Full text and rfc822 format available.

Message #8 received at 45432 <at> debbugs.gnu.org (full text, mbox):

From: Jim Meyering <jim <at> meyering.net>
To: "Fred .Flintstone" <eldmannen <at> gmail.com>
Cc: 45432 <at> debbugs.gnu.org
Subject: Re: bug#45432: Use both --include and --exclude at the same time
Date: Mon, 4 Jan 2021 09:55:48 -0800
tags 45432 moreinfo
stop

On Fri, Dec 25, 2020 at 8:57 AM Fred .Flintstone <eldmannen <at> gmail.com> wrote:
> It seems --exclude does nothing when --include is used. It would be useful
> to be able to use both together, in order to do things such as recusively
> grepping files of a certain file extension while excluding certain
> directories.
>
> Example:
> $ grep --recursive --include="*.cs" --exclude="*/tests/*"

Can you provide a complete example showing a malfunction?
You've probably already read this from "info grep", but see also
the description of --exclude there:

‘--include=GLOB’
     Search only files whose name matches GLOB, using wildcard matching
     as described under ‘--exclude’.  If contradictory ‘--include’ and
     ‘--exclude’ options are given, the last matching one wins.  If no
     ‘--include’ or ‘--exclude’ options match, a file is included unless
     the first such option is ‘--include’.




Information forwarded to bug-grep <at> gnu.org:
bug#45432; Package grep. (Tue, 05 Jan 2021 13:28:01 GMT) Full text and rfc822 format available.

Message #11 received at 45432 <at> debbugs.gnu.org (full text, mbox):

From: Norihiro Tanaka <noritnk <at> kcn.ne.jp>
To: Jim Meyering <jim <at> meyering.net>
Cc: "Fred .Flintstone" <eldmannen <at> gmail.com>, 45432 <at> debbugs.gnu.org
Subject: Re: bug#45432: Use both --include and --exclude at the same time
Date: Tue, 05 Jan 2021 22:27:30 +0900
On Mon, 4 Jan 2021 09:55:48 -0800
Jim Meyering <jim <at> meyering.net> wrote:

> tags 45432 moreinfo
> stop
> 
> On Fri, Dec 25, 2020 at 8:57 AM Fred .Flintstone <eldmannen <at> gmail.com> wrote:
> > It seems --exclude does nothing when --include is used. It would be useful
> > to be able to use both together, in order to do things such as recusively
> > grepping files of a certain file extension while excluding certain
> > directories.
> >
> > Example:
> > $ grep --recursive --include="*.cs" --exclude="*/tests/*"
> 
> Can you provide a complete example showing a malfunction?
> You've probably already read this from "info grep", but see also
> the description of --exclude there:
> 
> ‘--include=GLOB’
>      Search only files whose name matches GLOB, using wildcard matching
>      as described under ‘--exclude’.  If contradictory ‘--include’ and
>      ‘--exclude’ options are given, the last matching one wins.  If no
>      ‘--include’ or ‘--exclude’ options match, a file is included unless
>      the first such option is ‘--include’.

I understand as he requests "AND" condition.

$ mkdir a b
$ touch a/x.a a/x.b b/x.a b/x.b
$ for file in */*; do echo x >$file; done

Current result:
$ grep --recursive '--include=*.a' '--exclude=b/*' x .
./b/x.a:x
./a/x.a:x

Request from him:
$ grep --recursive '--include=*.a' '--exclude=b/*' x .
./a/x.a:x

It means "*.a expept b/*"





Information forwarded to bug-grep <at> gnu.org:
bug#45432; Package grep. (Wed, 06 Jan 2021 22:09:02 GMT) Full text and rfc822 format available.

Message #14 received at 45432 <at> debbugs.gnu.org (full text, mbox):

From: "Fred .Flintstone" <eldmannen <at> gmail.com>
To: Norihiro Tanaka <noritnk <at> kcn.ne.jp>
Cc: Jim Meyering <jim <at> meyering.net>, 45432 <at> debbugs.gnu.org
Subject: Re: bug#45432: Use both --include and --exclude at the same time
Date: Wed, 6 Jan 2021 21:12:09 +0100
[Message part 1 (text/plain, inline)]
Yes, I meant like Tanaka said, I want BOTH to include AND exclude.
So I can include only a file extension, and exclude certain directories
such as directories containing unit tests and integration tests.

On Tue, Jan 5, 2021 at 2:27 PM Norihiro Tanaka <noritnk <at> kcn.ne.jp> wrote:

>
> On Mon, 4 Jan 2021 09:55:48 -0800
> Jim Meyering <jim <at> meyering.net> wrote:
>
> > tags 45432 moreinfo
> > stop
> >
> > On Fri, Dec 25, 2020 at 8:57 AM Fred .Flintstone <eldmannen <at> gmail.com>
> wrote:
> > > It seems --exclude does nothing when --include is used. It would be
> useful
> > > to be able to use both together, in order to do things such as
> recusively
> > > grepping files of a certain file extension while excluding certain
> > > directories.
> > >
> > > Example:
> > > $ grep --recursive --include="*.cs" --exclude="*/tests/*"
> >
> > Can you provide a complete example showing a malfunction?
> > You've probably already read this from "info grep", but see also
> > the description of --exclude there:
> >
> > ‘--include=GLOB’
> >      Search only files whose name matches GLOB, using wildcard matching
> >      as described under ‘--exclude’.  If contradictory ‘--include’ and
> >      ‘--exclude’ options are given, the last matching one wins.  If no
> >      ‘--include’ or ‘--exclude’ options match, a file is included unless
> >      the first such option is ‘--include’.
>
> I understand as he requests "AND" condition.
>
> $ mkdir a b
> $ touch a/x.a a/x.b b/x.a b/x.b
> $ for file in */*; do echo x >$file; done
>
> Current result:
> $ grep --recursive '--include=*.a' '--exclude=b/*' x .
> ./b/x.a:x
> ./a/x.a:x
>
> Request from him:
> $ grep --recursive '--include=*.a' '--exclude=b/*' x .
> ./a/x.a:x
>
> It means "*.a expept b/*"
>
>
[Message part 2 (text/html, inline)]

Information forwarded to bug-grep <at> gnu.org:
bug#45432; Package grep. (Thu, 07 Jan 2021 06:17:01 GMT) Full text and rfc822 format available.

Message #17 received at 45432 <at> debbugs.gnu.org (full text, mbox):

From: Paul Eggert <eggert <at> cs.ucla.edu>
To: "Fred .Flintstone" <eldmannen <at> gmail.com>,
 Norihiro Tanaka <noritnk <at> kcn.ne.jp>
Cc: 45432 <at> debbugs.gnu.org
Subject: Re: bug#45432: Use both --include and --exclude at the same time
Date: Wed, 6 Jan 2021 22:16:41 -0800
On 1/6/21 12:12 PM, Fred .Flintstone wrote:
> Yes, I meant like Tanaka said, I want BOTH to include AND exclude.
> So I can include only a file extension, and exclude certain directories
> such as directories containing unit tests and integration tests.

grep -r --include='*.cs' --exclude-dir='tests' PATTERN DIR




Information forwarded to bug-grep <at> gnu.org:
bug#45432; Package grep. (Thu, 07 Jan 2021 06:54:02 GMT) Full text and rfc822 format available.

Message #20 received at submit <at> debbugs.gnu.org (full text, mbox):

From: "Paul Jackson" <pj <at> usa.net>
To: bug-grep <at> gnu.org
Subject: Re: bug#45432: Use both --include and --exclude at the same time
Date: Thu, 07 Jan 2021 00:51:43 -0600
Being an old school geek, I recommend using the "find" command,
perhaps piped to "xargs grep", to control which files to grep.

The find command has a more general syntax for logically choosing
files, by any property of name, path or stat fields, with possible nested
and, or and not predicates, as needed.

However ... I learned how to use find, xargs and grep long before grep
had any such options as --include and --exclude, so I should probably
be ignored as not contributing to the current discussion.

Carry on ... <chuckle>.

P.S. -- Best use "find -print0" and "xargs -0", in order to avoid some
ancient and regrettable nasty irregularities in how xargs otherwise
handles embedded spaces and unbalanced quotation marks in the
input stream.

See my xargs wrapper for a convenient workaround for this nastiness:
http://thepythoniccow.us/x.c.txt

-- 
                Paul Jackson
                pj <at> usa.net




Information forwarded to bug-grep <at> gnu.org:
bug#45432; Package grep. (Thu, 07 Jan 2021 13:00:02 GMT) Full text and rfc822 format available.

Message #23 received at 45432 <at> debbugs.gnu.org (full text, mbox):

From: "Fred .Flintstone" <eldmannen <at> gmail.com>
To: Paul Eggert <eggert <at> cs.ucla.edu>
Cc: Norihiro Tanaka <noritnk <at> kcn.ne.jp>, 45432 <at> debbugs.gnu.org
Subject: Re: bug#45432: Use both --include and --exclude at the same time
Date: Thu, 7 Jan 2021 13:58:42 +0100
[Message part 1 (text/plain, inline)]
Thanks!

I didn't know about the --exclude-dir option.

On Thu, Jan 7, 2021 at 7:16 AM Paul Eggert <eggert <at> cs.ucla.edu> wrote:

> On 1/6/21 12:12 PM, Fred .Flintstone wrote:
> > Yes, I meant like Tanaka said, I want BOTH to include AND exclude.
> > So I can include only a file extension, and exclude certain directories
> > such as directories containing unit tests and integration tests.
>
> grep -r --include='*.cs' --exclude-dir='tests' PATTERN DIR
>
[Message part 2 (text/html, inline)]

bug closed, send any further explanations to 45432 <at> debbugs.gnu.org and "Fred .Flintstone" <eldmannen <at> gmail.com> Request was from Paul Eggert <eggert <at> cs.ucla.edu> to control <at> debbugs.gnu.org. (Fri, 08 Jan 2021 20:25:01 GMT) Full text and rfc822 format available.

bug archived. Request was from Debbugs Internal Request <help-debbugs <at> gnu.org> to internal_control <at> debbugs.gnu.org. (Sat, 06 Feb 2021 12:24:06 GMT) Full text and rfc822 format available.

This bug report was last modified 3 years and 78 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.