LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 10-20-2018, 12:05 PM   #1
AnneRanch
Member
 
Registered: Oct 2018
Posts: 198

Rep: Reputation: 12
How to find files containing specific text ?


How do I combine "find" -name looking for specific "pattern" using grep?

For example
find . -name "*177.cpp" | grep "FAILED"

finds nothing.
I know such files exists , so I must be using wrong syntax.

( Admitting user error should help to eliminate inquisitive "replies' )

Help would be appreciated.
 
Old 10-20-2018, 12:11 PM   #2
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,750

Rep: Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222
Why do you expect the response to include the string "FAILED"?

First, try your find command without piping to grep and see what happens.
 
Old 10-20-2018, 12:13 PM   #3
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,359
Blog Entries: 3

Rep: Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767
If you are trying to search the file names then this will do it:

Code:
find . -type f -name "*177.cpp" -print | grep "FAILED"
If you are trying to search the contents of the files then you'll need to read up on -exec

Code:
man find
as in

Code:
find . -type f -name "*177.cpp" -exec grep "FAILED" {} +
 
Old 10-20-2018, 12:19 PM   #4
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,359
Blog Entries: 3

Rep: Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767
Another option would be to use xargs

Code:
find . -type f -name "*177.cpp" -print0 | xargs -0 grep "FAILED"
See "man xargs"
 
Old 10-20-2018, 12:23 PM   #5
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
I read on xargs a few days ago, xargs vs -exec .. xarg was said to be faster, so if you got lots of like files...
 
Old 10-20-2018, 12:23 PM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,041

Rep: Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348
or probably grep -r FAILED would be sufficient
 
Old 10-20-2018, 01:42 PM   #7
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
Quote:
Originally Posted by AnneRanch View Post
How do I combine "find" -name looking for specific "pattern" using grep?

For example
find . -name "*177.cpp" | grep "FAILED"

finds nothing.
I know such files exists , so I must be using wrong syntax.

( Admitting user error should help to eliminate inquisitive "replies' )

Help would be appreciated.
i am going to assume you want to find the string FAILED inside the files:
Code:
find . -name "*177.cpp" -exec grep FAILED {} \;
 
Old 10-20-2018, 02:53 PM   #8
AnneRanch
Member
 
Registered: Oct 2018
Posts: 198

Original Poster
Rep: Reputation: 12
Quote:
Originally Posted by Turbocapitalist View Post
If you are trying to search the file names then this will do it:

Code:
find . -type f -name "*177.cpp" -print | grep "FAILED"
If you are trying to search the contents of the files then you'll need to read up on -exec

Code:
man find
as in

Code:
find . -type f -name "*177.cpp" -exec grep "FAILED" {} +
Not what I need, this replace "*" with DEFINE.
find . -type f -name "*.cpp" -exec grep "DEFINE" {} +

Example
I need to find "#DEFINE MISO *" in header files.

Please - no references to RTFM necessary , I asked for help in forum .
I am perfectly capable to read and "try this ... try that".
I am asking for actual solution, if possible.

I understand there are different ways to do this , but I wanted HELP on "find" and "grep".
 
Old 10-20-2018, 03:39 PM   #9
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Code:
userx@manjaroieo:~
$ find . -name "*.cpp" | xargs grep -r "#include <iostre*"

./scripts/C++/filenames.cpp:#include <iostream>
./scripts/C++/isStringEmpty.cpp:#include <iostream>
./scripts/C++/test.cpp:#include <iostream>
./scripts/C++/filenamess.cpp:#include <iostream>
./scripts/C++/sleepcheck.cpp:#include <iostream>
./scripts/C++/getfilenames.cpp:#include <iostream>
./scripts/C++/multilines.cpp:#include <iostream>
./scripts/C++/loadfilenames.cpp:#include <iostream>
quoting string gets results.

Last edited by BW-userx; 10-20-2018 at 03:51 PM.
 
Old 10-21-2018, 04:56 AM   #10
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,832

Rep: Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219
What is wrong with
Code:
find . -type f -name "*.cpp" -exec grep "DEFINE" {} +
?
Do you want to also search in *.h files?
Code:
find . -type f \( -name "*.cpp" -o -name "*.h" \) -exec grep "DEFINE" {} +
Note:
the ( ) grouping is needed because the default AND between find options has precedence over the -o OR.
The \( \) or "(" ")" escapes are needed to prevent the shell's interference.
History:
early GNU find had only -exec ... {} \;
that was much slower than -print0 | xargs -0 ...
with programs that take multiple arguments.
The -exec ... {} + comes from the Posix standard, works like the xargs, and should be even faster.
 
Old 10-21-2018, 05:16 AM   #11
l0f4r0
Member
 
Registered: Jul 2018
Location: Paris
Distribution: Debian
Posts: 900

Rep: Reputation: 290Reputation: 290Reputation: 290
Sorry but I don't understand your need. First you wrote your initial post in such a way we thought you were searching for pattern "FAILED"... and now you seem to search for "#DEFINE MISO"...
Quote:
Originally Posted by AnneRanch View Post
find . -type f -name "*.cpp" -exec grep "DEFINE" {} +
What the problem with that command? Isn't it what you are searching for?

Edit: OK, regarding the command, MadeInGermany got it first

Last edited by l0f4r0; 10-21-2018 at 05:18 AM.
 
Old 10-21-2018, 06:20 AM   #12
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,359
Blog Entries: 3

Rep: Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767
Quote:
Originally Posted by AnneRanch View Post
Not what I need,
It is, however, a working answer to what you initially wrote. If you define your task a solution can be provides ALONG WITH where to read up on it so you are able to advance rather than passively receive ready-made recipes. If you want the latter then there are other forums where you may negotiate a price first.

Anyway, MadeInGermany has provided you with a modified solution in post #10. If it is not what you are looking for then please be more specific in the details of your problem and its scope.
 
Old 10-21-2018, 10:47 AM   #13
AnneRanch
Member
 
Registered: Oct 2018
Posts: 198

Original Poster
Rep: Reputation: 12
Quote:
Originally Posted by Turbocapitalist View Post
It is, however, a working answer to what you initially wrote. If you define your task a solution can be provides ALONG WITH where to read up on it so you are able to advance rather than passively receive ready-made recipes. If you want the latter then there are other forums where you may negotiate a price first.

Anyway, MadeInGermany has provided you with a modified solution in post #10. If it is not what you are looking for then please be more specific in the details of your problem and its scope.
Close but no cigar.

I wanted to find "DEFINE" not "DEFINES".



PHP Code:
jim@jim-desktop:~$ find . -type f \( -name "*.cpp" --name "*.h" \) -exec grep "DEFINE" {} +
./
eclipse-workspace/VNA_4/src/MODULES/M_BCM2835_SPI_TFT/CBCM2835SPITFT.h:#ifndef DEFINES
./eclipse-workspace/VNA_4/src/MODULES/M_BCM2835_SPI_TFT/CBCM2835SPITFT.h:#define DEFINES
./eclipse-workspace/VNA_4/src/MODULES/M_SPI/CLASS_SPI_BAD.cpp:#include "C_SPI_DEFINES.h" // more defines (?)
./eclipse-workspace/VNA_4/src/MODULES/M_SPI/CLASS_SPI.cpp:#include "C_SPI_DEFINES.h" // more defines (?)
./eclipse-workspace/VNA_4/src/MODULES/M_SPI/CLASS_SPI.h:    * C_SPI_DEFINES.
 
Old 10-21-2018, 10:53 AM   #14
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,359
Blog Entries: 3

Rep: Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767
Quote:
Originally Posted by AnneRanch View Post
I wanted to find "DEFINE" not "DEFINES".
Then you will need to modify the pattern used by grep in your search. If you check the manual page for it,

Code:
man grep
You will see that either \b or \< and \> can be used to match the end (or start) of a word.

So,

Code:
... -exec grep "\bDEFINE\b" {} + ...
Please give more detail including sample data of desired input, output, and what to be excluded from the search results.
 
Old 10-22-2018, 01:08 AM   #15
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
Quote:
Originally Posted by Turbocapitalist View Post
You will see that either \b or \< and \> can be used to match the end (or start) of a word.
grep actually has a '-w' switch for that.

but i don't want that stupid cigar anyhow.
this thread is a fine example of how some people mistreat anyone who doesn't provide a spelled out perfect solution to a problem they haven't even defined properly.
 
2 members found this post helpful.
  


Reply



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
LXer: How to find all files with a specific text using Linux shell LXer Syndicated Linux News 0 11-30-2017 07:46 AM
Searching a specific directory for files containing a text string lothwen Linux - Newbie 1 11-20-2010 10:58 AM
How to find files containing specific text? SurfTurtle Linux - Newbie 7 01-05-2008 05:57 AM
How to find and change a specific text in a text file by using shell script Bassam Programming 1 07-18-2005 07:15 PM
Command line tools to Find files with specific text naps Linux - Software 5 11-15-2004 04:46 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 04:08 PM.

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