[Techtalk] real life kewl text-processing and regexps samples wanted

Akkana Peck akkana at shallowsky.com
Fri Jan 9 22:44:58 EST 2004


Elena Bevell writes:
> I like this recursive grep that I found somewhere:
> 
> find  . -type f -exec grep "thing-to-find" {} \;

I used to do that all the time.  But one problem I found with it was
that grep doesn't print its filename argument if there's only one file,
so that command will print out the lines that match but you still won't
know what file it's in.

I changed it slightly as so:
find  . -type f -exec grep "thing-to-find" {} /dev/null \;

grep is guaranteed not to find your pattern in /dev/null, but now it
has two filename arguments so it will preface any line found with
the filename.

Daniel writes:
>   find . -type f -print0 | xargs -0 grep "thing-to-find"

But later I switched to the xargs format Daniel suggests.  I still
add /dev/null in case there's only one file found (he had a different
solution for that problem).  I'm not using -print0 and -0 but they
would probably be a good idea.

Here are my recursive grep aliases (for csh, bash syntax might require
different characters escaped) with explanations:

Recursive grep in anything that isn't an object file or library:
alias       gr      "find . -type f -and -not -name '*.o' -and -not -name '*.so' -and -not -name '*.a' | xargs grep \!* /dev/null"

Recursive grep only in C or C++ source code (very useful when looking
for bugs in unfamiliar code):
alias       cgr     "find . -name '*.h' -or -name '*.c' -or -name '*.cpp' -or -name '*.cc' | xargs grep \!* /dev/null"

Look only in header files (particularly useful starting in /usr/include):
alias       hgr     "find . -name '*.h' -or -name '*.idl' | xargs grep \!* /dev/null"

alias       jcgr    "find . -name '*.h' -or -name '*.c' -or -name '*.cpp' -or -name '*.cc' -or -name '*.js' | xargs grep \!* /dev/null"

Look only in html files (useful in my website, skipping binaries/scripts):
alias       htg     "find . -name '*.*htm*' | xargs grep \!* /dev/null"

Well, you get the idea, no need to list them all.

	...Akkana


More information about the Techtalk mailing list