yorua007
yorua007

Reputation: 833

the usage of grep --include option?

I want to find all occurrences of ncread in every .m files under the current directory and its sub-directories. And I use the following command:

grep -R --include="\.m" ncread .

But the command returns nothings. The manpage of grep said that:

--include=GLOB
  Search only files whose basename matches GLOB

Why it does not work ? Thank you!

Upvotes: 3

Views: 7573

Answers (4)

eltomito
eltomito

Reputation: 384

In case anybody is still interested, --include was broken in grep version 2.10 on my ubuntu precise and I fixed it by downloading a new version of grep (2.14) from the GNU site http://www.gnu.org/software/grep/#TOCdownloading and compiling it from source.

-download the archive called grep-2.14.tar.xz
-unpack it: xz -d grep-2.14.tar.xz
-untar it: tar -xf grep-2.14.tar
-remove the garbage: rm grep-2.14.tar
-go to its directory: cd grep-2.14
-do: ./configure
-then: make
-and finally: sudo make install

Now run grep --version to make sure you have 2.14 installed now. If it still reports the old version, try closing the terminal window and opening a new one.

Upvotes: 2

Jeff Laughlin
Jeff Laughlin

Reputation: 11

Try this

grep -R --include="\\.m" ncread *

The grep manpage says the include pattern is a GLOB, but at least on my cygwin system, I have confirmed experimentally that it's actually a regular expression.

Upvotes: 1

dying_sphynx
dying_sphynx

Reputation: 1166

glob is not a regexp, therefore you need to use glob syntax instead of regex syntax: --include='*.m' (note single quotes: you'd like to escape the glob to avoid expansion by your shell)

Glob characters summary:

* - any number of any characters
? - any single character
[abc] - single 'a', 'b' or 'c' character
\ - escaping, e.g. \* = single '*' character

You can start reading about glob here, if you need more details: Wiki page about glob

Upvotes: 5

hroptatyr
hroptatyr

Reputation: 4829

Because the basename of, say, foobar.m is foobar.

Use find and grep like so:

find -name "*.m" | xargs grep "ncread"

or if you happen to have whitespace in your filenames:

find -name "*.m" -print0 | xargs -0 grep "ncread"

Edit:
Here's how to do it with grep:

grep -R --include='*.m' ncread .

Upvotes: 5

Related Questions