views:

50

answers:

4

Hello,

Im using Ubuntu Karmic as my operating system . I frequently need to search my project folder for a particular string, to see if its there in any of the files in the project folder or its subfolders. I currently use the find command to do that, and have written a script that accepts the string im looking for as the parameter.

find . -exec grep -l $1 {} \;

But the problem with this is that it does not work with strings having a space in them. So, is there any way to search for space separated strings as well, or is there any available tool that does the job ?

Thank You.

A: 

Not an answer to your exact question, but I believe you may simply do grep -lR ${1} . to achieve what you need.

Pawel Marciniak
+1  A: 

A better version of that command is simply grep -rl "$1" ., or possibly grep -rl "$*" ..

If your string contains the correct amount of space, and the problem is simply the shell parsing the arguments, then you can refer to every arg with "$*" and you can prevent the shell from breaking at word boundaries (but still allow parameter expansion) by using the soft double-quotes.

DigitalRoss
A: 
grep -R "phrase with spaces" /folder/folder
pcp
+1  A: 

How are you invoking your script?

If you want to search for space separated strings you need to do the
invocation in the form:

%./script_name.sh 'search string'

and also change the find invocation to :

find . -exec grep -l "$1" {} \;

sateesh
Thanks sateesh...i think that worked :)
johnswr
@johnswr Welcome to SO - if an answer is helpful, you can thank the person answered by clicking on the checkmark near the answer. And once you have 15 reps, you can upvote him too. This way they earn reputation points and you get better accept rate which means better responses for your future questions.
Amarghosh