LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Searching multiple words? (https://www.linuxquestions.org/questions/linux-newbie-8/searching-multiple-words-850595/)

ritika_sharma 12-16-2010 02:38 AM

Searching multiple words?
 
How to search multiple words in multiple lines, inside a directory including sub-directory? Pls. give easy example.
I want to search the files (in /xx folder and all subfolders) that have header.h included and used x() function.
I tried $grep -r "header.h" | grep -r "x(" /Folder/subfolder/ > search.log

colucix 12-16-2010 02:50 AM

Maybe something like this is what you're looking for:
Code:

grep -lr 'header\.h' /xx/* | xargs grep -l 'x('
this prints out only the names of the files matching both the header.h and the x( patterns.

mmhs 12-16-2010 02:57 AM

i hope it help you

Quote:

grep -r " ""x(""/&"header.h" " /path

or

grep -r " ""x(""/|"header.h" " /path


ritika_sharma 12-16-2010 05:18 AM

Quote:

Originally Posted by mmhs (Post 4193245)
i hope it help you

Hi mmhs,
Sorry to say but both of above commands didn't work for me.

ritika_sharma 12-16-2010 09:29 PM

I think my question is not clear.
I want to search files (basically .cc files) in /xx folder and subfolders inside /xx folder.
Those files (actually, they are *.cc files) must have #include "header.h" AND x() function.

Clearly,
I wanna *.cc files that have 'header.h' & 'x()'.

I tried several ways, and got only files with header.h OR x().

colucix 12-17-2010 12:25 AM

Didn't my solution (above) work for you?

jschiwal 12-17-2010 12:47 AM

Code:

grep -lr 'header\.h' /xx/* | xargs grep -l 'x('
The part in bold is the base directory for your code.
You might want to add .cc after the star to only match .cpp files.
There needs to be at least on .cc file in your base target directory.

You could also use
find </path/to/directory> -type f -name "*.cc" | xargs grep -l 'x('

Try the left hand side command first to see if you are getting a list of files. If you are, then add the | xargs ... part.

colucix 12-17-2010 02:25 AM

Quote:

Originally Posted by jschiwal (Post 4194433)
You could also use
find </path/to/directory> -type f -name "*.cc" | xargs grep -l 'x('

This does not look for the header.h pattern, anyway. Or am I missing something? We need two grep statements, since I cannot find a suitable way to match two patterns on different lines using grep options or a more sophisticated regexp.


All times are GMT -5. The time now is 05:18 PM.