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.
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.
|
 |
03-14-2017, 11:15 AM
|
#1
|
Member
Registered: Nov 2015
Posts: 397
Rep: 
|
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?
Thank you.
|
|
|
03-14-2017, 12:08 PM
|
#2
|
LQ Guru
Registered: Mar 2016
Location: Harrow, UK
Distribution: LFS, AntiX, Slackware
Posts: 8,371
|
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.
|
|
1 members found this post helpful.
|
03-14-2017, 12:11 PM
|
#3
|
Senior Member
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278
|
Here is your stock standard normal grep output::
Code:
[root@tools test]# grep -Ri WORD
testdir14/somefile:WORD
testdir16/somefile:WORD
testdir17/somefile:WORD
testdir17/somefile:WORD
testdir17/somefile:WORD
testdir17/somefile:WORD
testdir17/somefile:WORD
testdir17/somefile:WORD
testdir17/somefile:WORD
testdir99/somefile:WORD
testdir93/somefile:WORD
Here is the same grep with each filename listed only once:
Code:
[root@tools test]# grep -Ri WORD|uniq
testdir14/somefile:WORD
testdir16/somefile:WORD
testdir17/somefile:WORD
testdir99/somefile:WORD
testdir93/somefile:WORD
Same with number of results in each file:
Code:
[root@tools test]# grep -Ri WORD|uniq -c
1 testdir14/somefile:WORD
1 testdir16/somefile:WORD
7 testdir17/somefile:WORD
1 testdir99/somefile:WORD
1 testdir93/somefile:WORD
Last edited by szboardstretcher; 03-14-2017 at 12:12 PM.
|
|
1 members found this post helpful.
|
03-14-2017, 12:35 PM
|
#4
|
Member
Registered: Nov 2015
Posts: 397
Original Poster
Rep: 
|
Thank you all.
I found this option:
Quote:
-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.
|
|
|
03-14-2017, 12:38 PM
|
#5
|
Senior Member
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278
|
Quote:
-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?
|
|
1 members found this post helpful.
|
03-14-2017, 12:56 PM
|
#6
|
Member
Registered: Nov 2015
Posts: 397
Original Poster
Rep: 
|
Quote:
Originally Posted by szboardstretcher
That work? Or do you mean 'filename only' and no path at all?
|
That works. Thanks.
Code:
~/dir00 $ grep -iRl "hello"
hello1
hello2
hello3
~/dir00 $ grep -il "hello" *
grep: dir: Is a directory
hello1
hello2
hello3
As for full pathname, I did not find any option for that.
|
|
|
03-14-2017, 01:34 PM
|
#7
|
LQ Veteran
Registered: Jul 2006
Location: London
Distribution: PCLinuxOS, Salix
Posts: 6,261
|
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
|
|
1 members found this post helpful.
|
03-14-2017, 02:08 PM
|
#8
|
Member
Registered: Nov 2015
Posts: 397
Original Poster
Rep: 
|
Quote:
Originally Posted by DavidMcCann
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
|
|
|
03-14-2017, 03:44 PM
|
#9
|
Member
Registered: Nov 2015
Posts: 397
Original Poster
Rep: 
|
What the heck, one more....
Quote:
~/dir00 $ find . -maxdepth 1 -type f | xargs grep -il "hello"
./hello1
./hello2
./hello3
|
|
|
|
All times are GMT -5. The time now is 07:34 AM.
|
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
|
|