Pages

grep search source code for specific string(s)

I had limited success incorporating this grep command into a bash script so I've documented it here so I can easily cut n' paste it in the future.

egrep -R -H -n -C1 --include=*.c --include=*.h 'searchstring'

If the search string contains spaces you need to use \s+ between the terms.

If the search string contains regex special characters they have to be escaped.

For example to search for "char *actual" the full search string becomes:

egrep -R -H -n -C1 --include=*.c --include=*.h 'char\s+\*actual'

Options:
-R to recurse
-H to show the file names
-n to show the line numbers
-C1 to show one line either side of the found line for context