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 |
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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
02-18-2009, 02:03 PM
|
#1
|
|
Member
Registered: Mar 2006
Distribution: Ubuntu 6.10
Posts: 112
Rep:
|
Grep an entire file but must contain multiple words
Hi,
Just wanted to get a bit better at my grep skills. Basically I want to return a filename if it contains the word 'element' and the word 'name' and the word 'foo' and the word 'bar'. If the file contains all these words, I want to know what file it is.
I have been trying the following which seems to work but it is ungodly slow. I will be searching a boatload of files so it would be nice if it were faster. I'm sure there is a built in better way for grep to handle this (some regex magic I dont know about).
$ find . -iname "*.txt" -print0 | xargs -0 -n 1 grep Tag | grep Read
This doesn't print out the filename, but kind of works. Suggestions for faster results and perhaps printing out the filename?
|
|
|
|
02-18-2009, 02:10 PM
|
#2
|
|
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,797
|
It's easy if the words can appear in order.....
Code:
<somecommands>|grep "*element*name*foo*bar*"
Lazy way out:
Code:
<somecommands>|grep element|grep name|grep foo|grep bar
(keywords must all be present and can be in any order)
Last edited by pixellany; 02-18-2009 at 02:12 PM.
|
|
|
|
02-18-2009, 04:54 PM
|
#3
|
|
Member
Registered: Jul 2006
Distribution: Debian, Ubuntu, openSUSE, CentOS
Posts: 147
Rep:
|
I would use something like:
Code:
find ./|grep -l <string>|xargs grep -l <string2>|xargs -l grep <string3> ...
|
|
|
|
02-18-2009, 07:45 PM
|
#4
|
|
Moderator
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,902
|
Code:
find -type f | awk '/foo/ && /bar/ && /baz/'
|
|
|
|
02-18-2009, 08:20 PM
|
#5
|
|
LQ Veteran
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 11,220
|
OP wants the filename - and won't you need to -exec that Tink ?
Code:
find -type f -exec awk '{if (/foo/ && /bar/ && /baz/) print FILENAME}' {} \;
|
|
|
|
02-18-2009, 08:33 PM
|
#6
|
|
Moderator
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,902
|
Hmmm ... I must have misunderstood him ... I thought he was looking
for files with foo, bar and baz in the name. :}
If he's looking for files that have foo, bar and baz inside them (anywhere)
and then print the filename none of the approaches above will quite do ...
they'll only work if foo, bar and baz are on one line, not anywhere in
the file ...
That'll be more like?
Code:
find -type f -exec awk 'BEGIN{foo=0;bar=0;baz=0}/foo/{foo++}/bar/{bar++}/baz/{baz++}END{if(foo>0 && bar>0 && baz>0){print FILENAME}}' {} \;
Completely untested, of course... 
|
|
|
|
02-18-2009, 09:57 PM
|
#7
|
|
Member
Registered: Sep 2007
Location: Las Vegas, NV
Distribution: Fedora / CentOS
Posts: 674
Rep:
|
good job! works great! Never used awk that way so I'll be storing that little goodie away for future use!
|
|
|
|
02-19-2009, 01:43 PM
|
#8
|
|
Member
Registered: Mar 2006
Distribution: Ubuntu 6.10
Posts: 112
Original Poster
Rep:
|
That is precisely what I was looking for, thank you very much for your solution. I'll chalk awk up on my list of programs to learn (it looks quite large)
|
|
|
|
02-19-2009, 01:58 PM
|
#9
|
|
Member
Registered: Mar 2006
Distribution: Ubuntu 6.10
Posts: 112
Original Poster
Rep:
|
Any ideas for how to get this in a script so it could be used like the following:
find . -iname "*.whatever" -type f | myScript any number of strings
this would be super useful. I can of course read a bunch and do my best as well 
|
|
|
|
02-19-2009, 02:11 PM
|
#10
|
|
Moderator
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,902
|
Now I won't sit down and write this for you ... ;} ... sorry!
But I'd devise a shell script that evaluates a switch -t (for type)
for your "*whatever", and accepts everything else on the command line
as your "any number of strings".
Then (in pseudo code) I'd
write "any number of strings" to a stringfile, one per line.
invoke find withe the "whatever" parameter.
have an awk script that reads stringfile, and assigns the
elements to an array in the BEGIN section.
for each line that awk processes iterate over the array and
check whether any item is part of the line, if so increment
it.
in the END section loop over the array, skip the record as
soon as a zero value occurs. If the loop doesn't exit, print
the file name.
|
|
|
|
02-19-2009, 04:46 PM
|
#11
|
|
LQ Veteran
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 11,220
|
I was thinking at the time 3 was about the limit for that approach and it was about time for a file of values and an array. @wakeboarder3780 do a google on "awk" an "associative array" to get an idea of what you can do - for example this article.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 12:55 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|