for you linux/bsd junkies out there....

JustinHoMi

Registered
What's the best way to grep the results of a find? I'm assuming you pipe the results into grep, but I don't know how to do that :)

For instance, I want to grep "test" RESULTSOFFIND
where RESULTSOFFIND is find / -name * -print

Any suggestions? =)

Justin
 
If you want to grep the results of any command, you just need to add this to the end of the command:

| grep test

or substitute what you're looking for instead of 'test'. Also, grep -i will do case-insensitive, and grep -v will return everything BUT what you specified.

So, in your example, you want to type this:

find / -name * -print | grep test


and if you want to include everything that has "test" but not "apple" you can do this:

find / -name * -print | grep test | grep -v apple
 
Well yeah, the answer is technically correct but
I wonder if that is what was intended.

A construction like

find / -name * -print | grep test

lists all the file names and then filters out only
those file names that have the word "test" in
them (and not for example Test or TEST since the -i
flag was not specified for grep).

Another way to interpret the original question may
be "How do I perform a grep through a large number
of files that are located throughout an entire
hierarchical directory structure", for example,
what source code uses this particular function.

Well here Unix gets rather tricky, in theory you
should be able to use the infamous backtick:
it executes the statement inside the ticks and
returns a list with the result, so:

grep test `find / -name *`

Cute and perfectly legal syntax and it works,
kind of, the smarties who implemented the shell
thought it was a great idea to cache the results
in a fixed size bufer, i.e. a construction like
this works as long as the total number of files
is not that great. Guess how many shell scripts have
been put into production only to fail some months later
when the number of files increased.

So... then there was the infamous xargs, which
basically is a hack to get around this, enter:

find / -name * | xargs grep test

the program xargs accepts input from standard input
and executes the command following it every time
it has a number of words. So the grep statement
is executed many times in stead of once in the
previous example.

Then the unix gods got even smarter and decided
to add a "-r" flag to grep, so now you can just
do:
cd / # Go to your directory
grep -r test . # Don't forget the dot !


So... finally.. the real cool thing..
if you do this from within emacs, by using a
compile, you can use the ctrl-x ` command
to step to each of the results one at a time.

For example, say you do search for a function
name in any of the files in the current directory
and everything below that (e.g. header directories,
c++ directories), you enter:

esc-x compile [return]
grep -r -n test .

The results will show up in a separate buffer,
while it is still searching (or when it is
finished) you can type ctrl-x ` to visit
each of the locations, emacs will read the
appropriate file in a buffer and position the cursor
at the appropriate line.


Apple rulez, so does Unix, so does emacs
 
Originally posted by andredeb
Well yeah, the answer is technically correct but
I wonder if that is what was intended.

Hey, the original post specifically mentioned "pipe the results into grep" so that's what I told him how to do. :) And I did mention the -i option; I just didn't include it in my examples.
 
Back
Top