[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Search for string in files



On Sat, 25 Aug 2007 17:37:05 -0500, Neil Gunton wrote:

>> I'm trying to figure out how to find a certain string inside a bunch of
>> files. If I, for examples, look for a certain function in a large source
>> tree, I could do
>> 
>> cat `find . -name '*.c'` | grep 'a_certain_function'
>> 
>> but this seems quite awkward, furthermore it doesn't help that much
>> because I don't know in which file the string was found. Maybe there's a
>> tool that makes it possible to find a string in a bunch of files and
>> also to list in which file the string was found? Or any modification to
>> the command given above?
> 
> I wrote a perl script that does this (and other stuff)

I wrote such a perl script as well when in Window$. But now I stick to
plain *nix solution -- why bother a script if you can do it with pure 
*nix command. Try this:

 grep -Hn 'a_certain_function' `find . -name '*.c'`

If the find command returns over hundreds of files, which might yield
command line too long error, try this:

 find . -name '*.c' | xargs grep -Hn 'a_certain_function' 

You can also substitute grep with agrep for much more advanced searches:

 agrep  text search tool with support for approximate patterns

See, the combination of pure *nix command can solve every possible
problems you may have, whereas in window$, you have to have thousands of
individual tools for thousands of different tasks, and even with the same
task, say rename files, you may have to choose from hundreds of tools to
pick the one that suits your needs. Yuck.

HTH

-- 
Tong (remove underscore(s) to reply)
  http://xpt.sourceforge.net/techdocs/
  http://xpt.sourceforge.net/tools/



Reply to: