Sunday, April 29, 2012

find grep on Mac OS X

On linux machines I search files using find, egrep and xargs as follows:

  find . -name "*.cpp" | xargs -i egrep -iHn "some search string" {}

this outputs any matches with the filename and number and also disables case dependency.

On my Mac it doesn't work. I tried reverting to egrep -r (to search recursively) instead, but that doesn't work. It just fails silently too. I tried installing findutils with brew to see if that helped, as often gnu tools are more up to date in brew than in the Apple version, but that didn't help.

So after some fiddling I found that the syntax below works:


  find . -name "*.cpp" | xargs egrep -iHn "some search string"

Only subtly different!

Actually, hold up, this does not work for filenames that have spaces in them. :(

Try this instead:

find . -type f -print0 | xargs -0 egrep -iHn "some search string"

J.

1 comment:

mahasiswa teladan said...

hi...Im student from Informatics engineering nice article,
thanks for sharing :)