On my version of grep, the order of --include and --exclude on the command line seems to be important. The following command finds hits in all files, not just in the cmake ones:

   $ grep --exclude=filename --include=*.cmake "pattern" -r .

But reverse the exclude and include, and it all works correctly, only finding matches in the *.cmake files:

   $ grep --include=*.cmake --exclude=filename "pattern" -r .

The same effect does not apply to --exclude-dir. I couldn't find any mention of this in the manual or on-line.

You might wonder why this matters, because I don't need the --exclude if "filename" doesn't match *.cmake: I have for some time had an alias like this:

   $ alias grep='grep --color=auto --exclude-dir={generated,.svn}
   --exclude=tags -I'

This all worked nicely, except that I could never make --include work correctly. I can't put the --exclude=tags option in the alias if I ever want to use --include. There is another approach, though:

   $ GREP_OPTIONS='--exclude-dir={generated,.svn} --exclude=tags -I'

Don't export GREP_OPTIONS, as doing so might trip up scripts that use grep. I only want the GREP_OPTIONS to apply on my command-line.

Just to add to the confusion, grep doesn't seem to respect --color=auto when it is in the GREP_OPTIONS, so I've had to leave that one in an alias:

   $ alias grep='grep --color=auto'

The equivalent work-around in Vim is to drop the --exclude onto the grep command-line after the :grep parameters from the user:

   :set grepprg=grep -HIn --exclude-dir={generated,.svn} $* --exclude=tags

Grep 2.14 on Ubuntu 13.04.
Douglas.
_______________________________________________
Linux-users mailing list
[email protected]
http://lists.canterbury.ac.nz/mailman/listinfo/linux-users

Reply via email to