LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 12-16-2010, 02:38 AM   #1
ritika_sharma
LQ Newbie
 
Registered: Dec 2010
Distribution: Ubuntu, Fedora
Posts: 7

Rep: Reputation: 0
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
 
Old 12-16-2010, 02:50 AM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,508

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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.
 
Old 12-16-2010, 02:57 AM   #3
mmhs
Member
 
Registered: Oct 2010
Posts: 99

Rep: Reputation: 1
i hope it help you

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

or

grep -r " ""x(""/|"header.h" " /path
 
0 members found this post helpful.
Old 12-16-2010, 05:18 AM   #4
ritika_sharma
LQ Newbie
 
Registered: Dec 2010
Distribution: Ubuntu, Fedora
Posts: 7

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by mmhs View Post
i hope it help you
Hi mmhs,
Sorry to say but both of above commands didn't work for me.
 
Old 12-16-2010, 09:29 PM   #5
ritika_sharma
LQ Newbie
 
Registered: Dec 2010
Distribution: Ubuntu, Fedora
Posts: 7

Original Poster
Rep: Reputation: 0
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().
 
Old 12-17-2010, 12:25 AM   #6
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,508

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Didn't my solution (above) work for you?
 
Old 12-17-2010, 12:47 AM   #7
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
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.
 
Old 12-17-2010, 02:25 AM   #8
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,508

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Quote:
Originally Posted by jschiwal View Post
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.
 
  


Reply

Tags
awk, cat, find, grep, search


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help on syntax for searching "all words" stabul LQ Suggestions & Feedback 12 10-20-2010 10:07 AM
grep, sed, awk or tr - searching words in a string hal8000b Programming 2 03-06-2009 08:04 PM
Grep an entire file but must contain multiple words wakeboarder3780 Linux - Newbie 10 02-19-2009 04:46 PM
searching for words in konqueror's .pdf-preview ungua Linux - Software 2 11-12-2006 01:35 PM
Giving multiple words as argument for bash ivanatora Linux - General 5 01-07-2004 02:04 AM


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

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration