LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 07-27-2010, 02:22 PM   #1
cedardoc
LQ Newbie
 
Registered: Jul 2010
Distribution: Ubuntu 10.04
Posts: 5

Rep: Reputation: 0
grep multiple words any order (AND, not OR) single line, from many files


Hi, I'm just trying to figure out how to use grep and am having trouble finding the way to return the lines of text that have both "word1" and "word2" but in any order.

this thread:
http://www.linuxquestions.org/questi...-words-705681/

kind of shows a similar thing in awk, but I'd like to use grep if possible as it seems to be way faster, and the awk example near the end only returns filenames, and I need to see the actual text. (however, even if that's easily solved, (I'm sure it must be) I'd still rather use grep).

Anyone figured this one out?
Thanks
 
Old 07-27-2010, 02:31 PM   #2
cedardoc
LQ Newbie
 
Registered: Jul 2010
Distribution: Ubuntu 10.04
Posts: 5

Original Poster
Rep: Reputation: 0
I found it:

egrep '(word1.*word2)|(word2.*word1)' *


Thanks again to Darth Wavy over at BashScripts.org
 
1 members found this post helpful.
Old 07-27-2010, 02:33 PM   #3
MensaWater
Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 5,943
Blog Entries: 5

Rep: Reputation: 753Reputation: 753Reputation: 753Reputation: 753Reputation: 753Reputation: 753Reputation: 753
The easiest way is simply to pipe one grep into another:

Code:
grep word1 file |grep word2
The output will only contain lines that have both word1 and word2. File is the file that you want to search for the pattern. If no file you would simply pipe your output into the first grep. e.g.
Code:
ls -l |grep word1 |grep word2
 
Old 07-27-2010, 02:35 PM   #4
Brains
Member
 
Registered: Apr 2009
Distribution: Debian testing
Posts: 258

Rep: Reputation: 42
Code:
grep word1 fromfile.file | less && grep word2 fromfile.file | less.
I also include pipping the results to less in case the words occur at a high rate, at least with less you can scroll through the output. Without less, it will pump it to standard out all at once.
 
Old 07-27-2010, 07:21 PM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 240Reputation: 240Reputation: 240
what if you have more than 2 words to search? use awk

Code:
awk '/word1/&&/word2/&&/word3/&&/word4/' file
 
1 members found this post helpful.
Old 07-27-2010, 08:10 PM   #6
cedardoc
LQ Newbie
 
Registered: Jul 2010
Distribution: Ubuntu 10.04
Posts: 5

Original Poster
Rep: Reputation: 0
Actually I did come across piping early in my search, but because I'm looking for both words being in the same line, that didn't work.

Code:
grep word1 * |grep word2

It would find the files with word1, and then show the occurrences of word2 from among those files.

I like the "egrep" one because it works and is fast :-)
 
Old 07-28-2010, 12:29 AM   #7
Brains
Member
 
Registered: Apr 2009
Distribution: Debian testing
Posts: 258

Rep: Reputation: 42
'Tis very rare I use grep, never had the opportunity or need to use egrep. But because of your fondness for it, the next time I do need grep, egrep will pop into mind and will be investigated....
Damn!
Never even finished this post and I typed "man egrep" in a terminal, quite a long man page.
Quote:
In addition, three variant programs egrep, fgrep and rgrep are available. egrep is the same as grep -E. fgrep is the same as grep -F.
rgrep is the same as grep -r. Direct invocation as either egrep or fgrep is deprecated, but is provided to allow historical applications
that rely on them to run unmodified.
 
Old 07-29-2010, 10:23 AM   #8
MensaWater
Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 5,943
Blog Entries: 5

Rep: Reputation: 753Reputation: 753Reputation: 753Reputation: 753Reputation: 753Reputation: 753Reputation: 753
Quote:
Originally Posted by cedardoc View Post
Actually I did come across piping early in my search, but because I'm looking for both words being in the same line, that didn't work.

Code:
grep word1 * |grep word2

It would find the files with word1, and then show the occurrences of word2 from among those files.

I like the "egrep" one because it works and is fast :-)
Maybe but I missed that you were doing a list of files - I posted before your egrep solution. If I'd thought through doing a list of files I would have told you to do the following to suppress file names.
Code:
grep -h word1 * |grep word2
By the way it doesn't just give you the file in the original - if gives you the line including the filename so when it does the second grep it IS doing it on the line the first grep returned (either with or without the -h).

There's no denying egrep (a/k/a grep -E) is a very useful tool - I use it all the time for OR searches and other purposes.

Last edited by MensaWater; 07-29-2010 at 10:39 AM.
 
  


Reply


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
Script to isolate files with 1 instance of each of 4 diff. words (grep) kmkocot Linux - Newbie 3 08-24-2009 08:54 AM
Grep an entire file but must contain multiple words wakeboarder3780 Linux - Newbie 10 02-19-2009 04:46 PM
Grep words and paste is on the same line say_hi_ravi Programming 7 10-31-2008 06:56 AM
Need to strip words from front of line. sed/awk/grep? joadoor Linux - Software 6 08-28-2006 04:39 AM
logrotate log files - keeping in order when using grep or cat and bash dmellem Linux - Software 6 02-11-2005 01:05 PM


All times are GMT -5. The time now is 02:39 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