LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   finding words in files. (https://www.linuxquestions.org/questions/linux-newbie-8/finding-words-in-files-803682/)

ukwho 04-23-2010 12:48 AM

finding words in files.
 
Hello,

I am looking for this `struct messages_sdd_t` and I need to search through a lot of *.c files to find it. However, I can't seen to find a match as I want to exclude all the words 'struct' and 'messages_sdd_t'. As I want to search on this only 'struct messages_sdd_t' The reason for this is, as struct is used many times and I keep getting pages or search results.

The directory I am searching in, has another directories so it will have to search recursively.

I have been doing this without success:
Code:

find . -type f -name '*.c' | xargs grep 'struct messages_sdd_t'
and this
Code:

find . -type f -name '*.c' | xargs egrep -w 'struct|messages_sdd_t'
Many thanks for any suggestions,

paulsm4 04-23-2010 01:03 AM

My first thought was to give you the "find .. -exec {} \;" syntax.

That would work ... but a better idea might be to acquaint you with "gid" ("id-utils").

Please do this:
1. See if you can find "id-utils" in your package manager.
Install it.
Otherwise, you can download and build the tarball from here.

2. "cd" to the top-level of your project, and type "mkid"
<= this creates an id-utils index of your entire source tree.
it just takes a few moments - even for a large project

3. Type "gid messages_sdd_t"
You should see all instances of "messages_sdd_t", including the definition.

4. You can combine "gid XYZ" with "less", "grep" and other tools easily to filter your search.
You can use id-utils with "vi" or "emacs" to lookup code directly in your text editor.

'Hope that helps .. PSM

PS:
find . -name "*.c" -exec {} grep messages_sdd_t \;

kainosnous 04-23-2010 03:44 AM

When I try your first example, I get the expected results, so I'm not sure what is not working. You may want to try using a regular expression for more control like this:

Code:

find . -type f -name '*.c' | xargs grep -e "struct.*messages_sdd_t'
That would list any line with "struct" and then "messages_sdd_t"

paulsm4 04-23-2010 09:36 AM

Try id-utils. Satisfaction guaranteed :)


All times are GMT -5. The time now is 11:42 AM.