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 03-14-2017, 11:15 AM   #1
fanoflq
Member
 
Registered: Nov 2015
Posts: 397

Rep: Reputation: Disabled
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.
 
Old 03-14-2017, 12:08 PM   #2
hazel
LQ Guru
 
Registered: Mar 2016
Location: Harrow, UK
Distribution: LFS, AntiX, Slackware
Posts: 7,049
Blog Entries: 17

Rep: Reputation: 4222Reputation: 4222Reputation: 4222Reputation: 4222Reputation: 4222Reputation: 4222Reputation: 4222Reputation: 4222Reputation: 4222Reputation: 4222Reputation: 4222
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.
Old 03-14-2017, 12:11 PM   #3
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1693Reputation: 1693Reputation: 1693Reputation: 1693Reputation: 1693Reputation: 1693Reputation: 1693Reputation: 1693Reputation: 1693Reputation: 1693Reputation: 1693
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.
Old 03-14-2017, 12:35 PM   #4
fanoflq
Member
 
Registered: Nov 2015
Posts: 397

Original Poster
Rep: Reputation: Disabled
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.
 
Old 03-14-2017, 12:38 PM   #5
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1693Reputation: 1693Reputation: 1693Reputation: 1693Reputation: 1693Reputation: 1693Reputation: 1693Reputation: 1693Reputation: 1693Reputation: 1693Reputation: 1693
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.
Old 03-14-2017, 12:56 PM   #6
fanoflq
Member
 
Registered: Nov 2015
Posts: 397

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by szboardstretcher View Post
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.
 
Old 03-14-2017, 01:34 PM   #7
DavidMcCann
LQ Veteran
 
Registered: Jul 2006
Location: London
Distribution: PCLinuxOS, Debian
Posts: 6,085

Rep: Reputation: 2279Reputation: 2279Reputation: 2279Reputation: 2279Reputation: 2279Reputation: 2279Reputation: 2279Reputation: 2279Reputation: 2279Reputation: 2279Reputation: 2279
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.
Old 03-14-2017, 02:08 PM   #8
fanoflq
Member
 
Registered: Nov 2015
Posts: 397

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by DavidMcCann View Post
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
 
Old 03-14-2017, 03:44 PM   #9
fanoflq
Member
 
Registered: Nov 2015
Posts: 397

Original Poster
Rep: Reputation: Disabled
What the heck, one more....

Quote:
~/dir00 $ find . -maxdepth 1 -type f | xargs grep -il "hello"
./hello1
./hello2
./hello3
 
  


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
sed removing everything before the second occurrence of a word forkbomb Linux - General 9 04-05-2016 07:17 AM
[SOLVED] Auto copy Firefox words in persdict.dat to other word files. linustalman Linux - Software 10 08-04-2014 04:00 AM
How to find the words in log files genROCK Linux - Newbie 9 10-19-2011 09:18 AM
Find/grep/wc command to find matching files, print filename and word count dbasch Linux - Newbie 10 09-14-2009 05:55 PM
shell script to find an word or words from a line rakesh.tandur Linux - General 5 05-13-2008 01:57 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:37 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration