Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find, grep, and execute - all in one?

Tags:

grep

find

shell

This is the command I've been using for finding matches (queryString) in php files, in the current directory, with grep, case insensitive, and showing matching results in line:

find . -iname "*php" -exec grep -iH queryString {} \;

Is there a way to also pipe just the file name of the matches to another script?

I could probably run the -exec command twice, but that seems inefficient.

What I'd love to do on Mac OS X is then actually to "reveal" that file in the finder. I think I can handle that part. If I had to give up the inline matches and just let grep show the files names, and then pipe that to a third script, that would be fine, too - I would settle.

But I'm actually not even sure how to pipe the output (the matched file names) to somewhere else...

Help! :)

Clarification

I'd like to reveal each of the files in a finder window - so I'm probably not going to using the -q flag and stop at the first one.

I'm going to run this in the console, ideally I'd like to see the inline matches printed out there, as well as being able to pipe them to another script, like oascript (applescript, to reveal them in the finder). That's why I have been using -H - because I like to see both the file name and the match.

If I had to settle for just using -l so that the file name could more easily be piped to another script, that would be OK, too. But I think after looking at the reply below from @Charlie Martin, that xargs could be helpful here in doing both at the same time with a single find, and single grep command.

I did say bash but I don't really mind if this needs to be ran as /bin/sh instead - I don't know too much about the differences yet, but I do know there are some important ones.

Thank you all for the responses, I'm going to try some of them at the command line and see if I can get any of them to work and then I think I can choose the best answer. Leave a comment if you want me to clarify anything more.

Thanks again!

like image 783
cwd Avatar asked May 21 '11 00:05

cwd


2 Answers

You bet. The usual thing is something like

  $ find /path -name pattern -print | xargs command

So you might for example do

  $ find . -name '*.[ch]' -print | xargs grep -H 'main' 

(Quiz: why -H?)

You can carry on with this farther; for example. you might use

  $ find . -name '*.[ch]' -print | xargs grep -H 'main' | cut -d ':' -f 1

to get the vector of file names for files that contain 'main', or

  $ find . -name '*.[ch]' -print | xargs grep -H 'main' | cut -d ':' -f 1 |
      xargs growlnotify -

to have each name become a Growl notification.

You could also do

 $ grep pattern `find /path -name pattern`

or

 $ grep pattern $(find /path -name pattern)

(in bash(1) at least these are equivalent) but you can run into limits on the length of a command line that way.

Update

To answer your questions:

(1) You can do anything in bash you can do in sh. The one thing I've mentioned that would be any different is the use of $(command) in place of using backticks around command, and that works in the version of sh on Macs. The csh, zsh, ash, and fish are different.

(2) I think merely doing $ open $(dirname arg) will opena finder window on the containing directory.

like image 91
Charlie Martin Avatar answered Oct 21 '22 04:10

Charlie Martin


It sounds like you want to open all *.php files that contain querystring from within a Terminal.app session.

You could do it this way:

find . -name '*.php' -exec grep -li 'querystring' {} \; | xargs open

With my setup, this opens MacVim with each file on a separate tab. YMMV.

like image 29
Johnsyweb Avatar answered Oct 21 '22 03:10

Johnsyweb