Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use grep -e to match partial words?

Tags:

linux

grep

bash

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!

like image 930
Flo Bayer Avatar asked Nov 13 '17 09:11

Flo Bayer


2 Answers

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.

like image 80
nyronium Avatar answered Nov 03 '22 15:11

nyronium


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
like image 25
sjsam Avatar answered Nov 03 '22 14:11

sjsam