grep multiple words any order (AND, not OR) single line, from many files
Linux - NewbieThis 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!
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
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).
'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....
Never even finished this post and I typed "man egrep" in a terminal, quite a long man page.
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.
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.
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.
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.