tarballed
October 22nd, 2003, 16:51
Hello everyone! Im trying to figure out a way to do the following:

I want to search files in multiple directories that contain a certain piece of text in them and have the output tell me which files contain the text.

That make sense?

So basically, I could search /etc and down and look for Dr. Zeus text, and then have the command tell me what files contain the text, Dr. Zeus.

Im thinking sed or awk, or a combo of both.

Any ideas?

tarballed
October 22nd, 2003, 17:11
I realize that I could probably do something like:

grep <stuff> *

But isnt there a better way to do something like this?

Tarballed

bsdjunkie
October 22nd, 2003, 17:12
If all you need to do is find the files where the txt exists, its probably easiest using grep.

grep 'Dr. Seus' * on whatever dir your looking in. If you want to find/replace or do other atcions, then sed/awk/perl are the way to go.

frisco
October 22nd, 2003, 17:57
Depends on exactly what you want to do and on what platform you want to do it.

grep -r <pattern> /path/to/dir

some OS's don't support 'grep -r', so you can:
find . -exec grep <pattern> \{\} /dev/null \;

You can get trickier if you know something about what you are looking for. Let's say i want to search for all occurences of gdt_get_slot in all C code on the system. I could:
find / -type f -print0 | xargs -0 file | grep 'C program text' | cut -f1 -d: | xargs grep gdt_get_slot

tarballed
October 22nd, 2003, 18:25
find / -type f -print0 | xargs -0 file | grep 'C program text' | cut -f1 -d: | xargs grep gdt_get_slot

I need to get better at using find. It has some very cool aspects of it. Also, im reading up on sed & awk. I like them both. Should server me well.

Now, I need to disect friscos command. :)

Tarballed

soup4you2
October 22nd, 2003, 18:45
more * | grep -l 'text here' *

molotov
October 23rd, 2003, 00:11
I suppose you could open every file with fopen, then use regeps to match what you want, but grep really is the way you want to go. (it does essentially the same thing, Global Regular Expression Parser after all ;-)