Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'git grep' and word boundaries on Mac OS X and BSD

I run git grep "\<blah\>" regularly on my linux development server, but I just discovered that I am not able to use \< and \> on Mac (Mac OS X 10.6.8) (not able to use = it does not find anything). Is the regular expressions syntax different in Mac?

I tried using git grep -E "\<blah\>" but to no avail! :-(

like image 685
Yogeshwer Sharma Avatar asked Sep 30 '11 08:09

Yogeshwer Sharma


People also ask

Does Macos have grep?

To locate a string within a file, use the grep tool. The grep tool searches the named input files for lines containing a match to the given pattern. By default, grep prints the matching lines.

Which statement is the best comparison between git grep and grep?

The git grep version will only search in files tracked by git, whereas the grep version will search everything in the directory.

What files are searchable by git grep?

`git grep` command is used to search in the checkout branch and local files. But if the user is searching the content in one branch, but the content is stored in another branch of the repository, then he/she will not get the searching output.

What is word boundary in regex?

A word boundary is a zero-width test between two characters. To pass the test, there must be a word character on one side, and a non-word character on the other side. It does not matter which side each character appears on, but there must be one of each.


1 Answers

After struggling with this, too, I found this very helpful post on a BSD mailing list. So here's the (albeit rather ugly) solution:

git grep "[[:<:]]blah[[:>:]]"

The -w flag of git-grep also works but sometimes you want to only match the beginning or end of a word.

Update: This has changed in OS X 10.9 "Mavericks". Now you can use \<, \>, and \b. [[:<:]] and [[:>:]] are no longer supported.

like image 176
nwellnhof Avatar answered Sep 28 '22 03:09

nwellnhof