LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Find a file with text "abc" (https://www.linuxquestions.org/questions/linux-newbie-8/find-a-file-with-text-abc-410128/)

snoozing 01-31-2006 04:28 PM

Find a file with text "abc"
 
I am looking for a find function which
will seek a file containing a text string.

Don't say grep because it doesn't work.

I'm looking for something I can do on the command line.

titopoquito 01-31-2006 04:34 PM

Tell us what you're doing -- grep _should_ do it. Maybe you forgot to include the insensitive search or the recursive option? Please post the command you entered at the command line.

pixellany 01-31-2006 04:42 PM

grep grep grep grep grep grep
Of course grep works---but it has to be used in conjuction with other things.
File is myfile
target string is xyz

cat myfile | grep xyz prints out each line of the file that contains "xyz" Returns exit status of 0 if the string is found at least once, and 1 if there were no matches.
Follow with echo $? to display the exit status.

Put all this together and you can write a script that tells you what files contain the string--and the number of occurences, if you desire.

To really get into this kind of thing, I suggest "Bash guide for Beginners", by Machtelet Garrels......tldp.org

This is inelegant and only one of many ways to do the same thing.

titopoquito 01-31-2006 05:00 PM

To add two simple searches for files containing xyz in the current directory and containing folders, both output just the filename, not the line containing the search pattern itself:

grep -R -l xyz *
find . -type f -exec grep -l xyz {} \; -- searches also in hidden directories and files

snoozing 02-01-2006 03:50 AM

Works now,
the cat filename | grep xyz
and the grep -R -l xyz *

I don't understand what went wrong before--stress.

find . -type f -exec grep -l xyz {} \; => error:find paths must precede expression
a pipe missing?

Dtsazza 02-01-2006 04:59 AM

Quote:

Originally Posted by pixellany
cat myfile | grep xyz

Is there any benefit/difference to grep reading from a pipe rather than from an input file, as in 'grep xyz myfile'?

Wim Sturkenboom 02-01-2006 05:44 AM

Quote:

Originally Posted by snoozing
find . -type f -exec grep -l xyz {} \; => error:find paths must precede expression
a pipe missing?

find . -type f -exec grep -l xyz {} \; works on my boxes (slack and rh). However, find . -name * -exec grep -l xyz {} \; gives the error message as well. Solution is to put the * between double quotes like find . -name "*" -exec grep -l xyz {} \; or to put a backslash before the * (see man find)

pixellany 02-01-2006 08:07 AM

Quote:

Originally Posted by Dtsazza
Is there any benefit/difference to grep reading from a pipe rather than from an input file, as in 'grep xyz myfile'?

Interesting question....does a pipe actually slow things down or is it just a syntactic variation that produces that same machine code?
One advantage of the way I wrote it could be readability for the beginner.

Wim Sturkenboom 02-01-2006 11:17 PM

Quote:

Originally Posted by pixellany
One advantage of the way I wrote it could be readability for the beginner.

Your view :) A real beginner does not know about pipes :study: , so the plain 'grep' is better in my view.

I ran a small test on a 200MByte text file. Don't know if it's really a reliable one:
Code:

time grep mytxt myfile
real 17.1
user 16.9
sys  0.16

time cat myfile | grep mytxt
real 17.0
user 16.7
sys  0.32

The commands were run a couple of times in random sequece (mixing both commands to 'eliminate' possible cache influences).

It surprised me that the 'cat' version actually was faster everytime it was executed. If other processes would have influenced the results, that would be equally true for both versions of the command.

PS: 'mytxt' only occured once as the first word on the last line
PS2: results are rounded


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