LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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, 01: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, 01:11 PM   #2
scasey
Senior Member
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.7.1908
Posts: 4,376

Rep: Reputation: 1551Reputation: 1551Reputation: 1551Reputation: 1551Reputation: 1551Reputation: 1551Reputation: 1551Reputation: 1551Reputation: 1551Reputation: 1551Reputation: 1551
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, 01:13 PM   #3
Turbocapitalist
Senior Member
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 4,457
Blog Entries: 3

Rep: Reputation: 2230Reputation: 2230Reputation: 2230Reputation: 2230Reputation: 2230Reputation: 2230Reputation: 2230Reputation: 2230Reputation: 2230Reputation: 2230Reputation: 2230
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, 01:19 PM   #4
Turbocapitalist
Senior Member
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 4,457
Blog Entries: 3

Rep: Reputation: 2230Reputation: 2230Reputation: 2230Reputation: 2230Reputation: 2230Reputation: 2230Reputation: 2230Reputation: 2230Reputation: 2230Reputation: 2230Reputation: 2230
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, 01:23 PM   #5
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware FreeBSD Win10 MX
Posts: 9,628

Rep: Reputation: 2073Reputation: 2073Reputation: 2073Reputation: 2073Reputation: 2073Reputation: 2073Reputation: 2073Reputation: 2073Reputation: 2073Reputation: 2073Reputation: 2073
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, 01:23 PM   #6
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 13,602

Rep: Reputation: 4361Reputation: 4361Reputation: 4361Reputation: 4361Reputation: 4361Reputation: 4361Reputation: 4361Reputation: 4361Reputation: 4361Reputation: 4361Reputation: 4361
or probably grep -r FAILED would be sufficient
 
Old 10-20-2018, 02:42 PM   #7
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 13,413
Blog Entries: 9

Rep: Reputation: 3659Reputation: 3659Reputation: 3659Reputation: 3659Reputation: 3659Reputation: 3659Reputation: 3659Reputation: 3659Reputation: 3659Reputation: 3659Reputation: 3659
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, 03: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, 04:39 PM   #9
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware FreeBSD Win10 MX
Posts: 9,628

Rep: Reputation: 2073Reputation: 2073Reputation: 2073Reputation: 2073Reputation: 2073Reputation: 2073Reputation: 2073Reputation: 2073Reputation: 2073Reputation: 2073Reputation: 2073
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 04:51 PM.
 
Old 10-21-2018, 05:56 AM   #10
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 1,293

Rep: Reputation: 595Reputation: 595Reputation: 595Reputation: 595Reputation: 595Reputation: 595
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, 06:16 AM   #11
l0f4r0
Member
 
Registered: Jul 2018
Location: Paris
Distribution: Debian
Posts: 896

Rep: Reputation: 288Reputation: 288Reputation: 288
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 06:18 AM.
 
Old 10-21-2018, 07:20 AM   #12
Turbocapitalist
Senior Member
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 4,457
Blog Entries: 3

Rep: Reputation: 2230Reputation: 2230Reputation: 2230Reputation: 2230Reputation: 2230Reputation: 2230Reputation: 2230Reputation: 2230Reputation: 2230Reputation: 2230Reputation: 2230
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, 11: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, 11:53 AM   #14
Turbocapitalist
Senior Member
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 4,457
Blog Entries: 3

Rep: Reputation: 2230Reputation: 2230Reputation: 2230Reputation: 2230Reputation: 2230Reputation: 2230Reputation: 2230Reputation: 2230Reputation: 2230Reputation: 2230Reputation: 2230
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, 02:08 AM   #15
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 13,413
Blog Entries: 9

Rep: Reputation: 3659Reputation: 3659Reputation: 3659Reputation: 3659Reputation: 3659Reputation: 3659Reputation: 3659Reputation: 3659Reputation: 3659Reputation: 3659Reputation: 3659
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


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

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

All times are GMT -5. The time now is 05:26 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration