Page 1 of 1

unable to use grep use grep command in Centos 7

Posted: 2020/06/16 14:33:49
by sharingan
Hi All,

currently i am using centos 7 , while using grep unable to get any result same issue with piping .
Please advice

Re: unable to use grep use grep command in Centos 7

Posted: 2020/06/16 14:37:08
by tunk
More info please, e.g. what are your commands?

Re: unable to use grep use grep command in Centos 7

Posted: 2020/06/16 14:55:05
by TrevorH
Be specific, what did you run, what did you expect to find, show the line from the target file containing the string you were looking for as well as the exact command you ran that did not work.

Re: unable to use grep use grep command in Centos 7

Posted: 2020/06/16 15:05:20
by sharingan
Hi

i using a simple searching and piping comnd : find . -type f | grep (character) .
But its showing no result no error message as well , for reference added the screen shot

Re: unable to use grep use grep command in Centos 7

Posted: 2020/06/16 15:47:12
by tunk
Your find command will list all files, not the content of them.
You could look at the fgrep command.

Re: unable to use grep use grep command in Centos 7

Posted: 2020/06/16 15:55:22
by MartinR
Please do not use screen shots, cut and paste into a code (</>) block. Otherwise anyone trying to help you has to copy type which is liable to put people off.

Now to your problem. If you do a plain:

Code: Select all

find . -type f
./copy2.txt
./copy3.txt
./test1.txt
You can see that the output of the find command is a list of file names, that is three strings Now think about the pipeline, it is passing this output to the input of the grep, essentially:

Code: Select all

echo "./copy2.txt" | grep we
I would hope it is clear that the substring "we" occurs nowhere in the string "./copy2.txt".

Try:

Code: Select all

$ find . -type f -exec grep we '{}' \;
We should be do that , we are going.
Note the '{}' which tells find to inset the file name at that point, the execute the command. Do not forget the \; at the end or find will not know where the command ends. Have a read of the man page, it's all explained there.

Re: unable to use grep use grep command in Centos 7

Posted: 2020/06/17 06:37:43
by sharingan
Thanks for the help , i got it perfectly , and i am very sorry for the attachment .

Re: unable to use grep use grep command in Centos 7

Posted: 2020/06/17 07:41:03
by jlehtone
Grep can recurse directory. No find is needed:

Code: Select all

grep -r we .

Re: unable to use grep use grep command in Centos 7

Posted: 2020/06/17 08:17:17
by MartinR
Neat. It only goes to show that us "old hands" ought to re-read the simple stuff from time to time - sometimes "that's the way I've done it for 20 years" is not always the most efficient! More seriously, that is great for simple searches, but remember that find has powerful selection mechanisms for more advanced searches.

Re: unable to use grep use grep command in Centos 7

Posted: 2020/06/17 09:26:34
by jlehtone
The "File and Directory Selection" of man grep has some options, but yes, far from what find can.

There must be a way to find out, when such features have been injected into the code. Open source and all.