Flo Bayer
Flo Bayer

Reputation: 1260

How to use grep -e to match partial words?

If you use grep like

grep -rnw '/your/path/to/search/' -e 'n getFoo'

grep wouldn't find a file that contains "function getFoo()".

If you only search for 'getFoo' or else 'function getFoo', grep will find the file that contains the function.

So what's the best way to find a file containing part(s) of a string?

Thanks in advance!

Upvotes: 10

Views: 27411

Answers (2)

sjsam
sjsam

Reputation: 21965

The -w flag causes whole word matches so it clearly what you don't need.

So what's the best way to find a file containing part(s) of a string?

shopt -s globstar
grep -li 'string to search for' /path/to/search/**
shopt -u globstar

Upvotes: 1

nyronium
nyronium

Reputation: 1278

You should remove the -w option which tells grep to only match whole words.

grep -rn '/your/path/to/search/' -e 'n getFoo'

will also search in between word boundaries.

Upvotes: 19

Related Questions