[SOLVED] Find occurrence of word (or a list of words) in files but not repeat printing
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!
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.
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.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Find occurrence of word (or a list of words) in files but not repeat printing
Grep can be used to find words in all files of a directory.
It then displays the line containing the word.
It prints as many lines as it finds for the same file.
This clutters up the screen badly.
I like it to print only once.
Here are possible scenarios:
a) Find a word in all files in a directory,
but only prints the filename once for word found,
and proceed to the next file for the search.
b) Find a list of words in all files in a directory,
but only prints the filename once
(for each word in the list when found),
and proceed to the next file
(when list of words is completed)
for the search.
Is there another command to do this other than grep?
Grep has a lot of different display options. You are seeing its default behaviour, which is to print every matching line. You can also print only the first matching line from each file or just the names of files that contain a match.
As a general principle, it's always worth reading the man page for a command if its default behaviour isn't satisfactory. This is especially the case for old unix commands like grep, which often have many and complex option sets.
-m NUM, --max-count=NUM
Stop reading a file after NUM matching lines. If the input is standard input from a regular file, and NUM matching lines are output, grep ensures that the standard input is positioned to just after the last matching line before exiting, regardless of the presence of trailing context lines. This enables a calling process to resume a search. When grep stops after NUM matching lines, it outputs any trailing context lines. When the -c or --count option is also used, grep does not output a count greater than NUM. When the -v or --invert-match option is also used, grep stops after outputting NUM non-matching lines.
There seems to be no option to print only filenames.
I guess we have to use awk for that.
-l, --files-with-matches
Suppress normal output; instead print the name of each input file from which output would normally have been printed. The scanning will stop on the
first match. (-l is specified by POSIX.)
That work? Or do you mean 'filename only' and no path at all?
If you just wanted to check which files have the word, how about
Code:
for file in path/*.txt; do grep -iq searchterm $file && echo $file; done
Thanks.
Works!
Code:
~ $ for file in ./dir00/*; do grep -iq "hello" $file && echo $file; done
grep: ./dir00/dir: Is a directory
./dir00/hello1
./dir00/hello2
./dir00/hello3
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.