LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   How to find files containing specific text ? (https://www.linuxquestions.org/questions/linux-software-2/how-to-find-files-containing-specific-text-4175640778/)

AnneRanch 10-20-2018 12:05 PM

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.

scasey 10-20-2018 12:11 PM

Why do you expect the response to include the string "FAILED"?

First, try your find command without piping to grep and see what happens.

Turbocapitalist 10-20-2018 12:13 PM

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" {} +

Turbocapitalist 10-20-2018 12:19 PM

Another option would be to use xargs

Code:

find . -type f -name "*177.cpp" -print0 | xargs -0 grep "FAILED"
See "man xargs"

BW-userx 10-20-2018 12:23 PM

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...

pan64 10-20-2018 12:23 PM

or probably grep -r FAILED would be sufficient

ondoho 10-20-2018 01:42 PM

Quote:

Originally Posted by AnneRanch (Post 5917147)
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 {} \;

AnneRanch 10-20-2018 02:53 PM

Quote:

Originally Posted by Turbocapitalist (Post 5917150)
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".

BW-userx 10-20-2018 03:39 PM

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.

MadeInGermany 10-21-2018 04:56 AM

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.

l0f4r0 10-21-2018 05:16 AM

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 (Post 5917213)
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 ;)

Turbocapitalist 10-21-2018 06:20 AM

Quote:

Originally Posted by AnneRanch (Post 5917213)
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.

AnneRanch 10-21-2018 10:47 AM

Quote:

Originally Posted by Turbocapitalist (Post 5917430)
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.


Turbocapitalist 10-21-2018 10:53 AM

Quote:

Originally Posted by AnneRanch (Post 5917494)
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.

ondoho 10-22-2018 01:08 AM

Quote:

Originally Posted by Turbocapitalist (Post 5917498)
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.


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