-   Linux - Newbie (
-   -   Searching multiple words? (

ritika_sharma 12-16-2010 03: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 03:50 AM

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

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 03:57 AM

i hope it help you


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


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

ritika_sharma 12-16-2010 06:18 AM


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 10: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.

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 01:25 AM

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

jschiwal 12-17-2010 01:47 AM


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 03:25 AM


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 04:38 PM.