Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Easiest way to do "git grep" for multiple strings?

Tags:

git

I often need to search for lines containing multiple strings/patterns in a git project e.g.

git grep -i -e str1 --and -e str2 --and -e str3 -- *strings*txt

This gets tedious very quickly.

Is there a better way to do this?

like image 991
Debajit Avatar asked Jul 09 '13 23:07

Debajit


People also ask

Can you grep multiple strings?

Grep is a powerful utility available by default on UNIX-based systems. The name stands for Global Regular Expression Print. By using the grep command, you can customize how the tool searches for a pattern or multiple patterns in this case. You can grep multiple strings in different files and directories.

Why is git grep faster than grep?

That's because git-grep doesn't need to bother to go through the filesystem for the on-disk files. It greps directly from the object storage instead. And AFAIK it can run the grep in parallel.

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.


2 Answers

I find it easiest to use extended regular expressions -E and | (or).

git grep -E 'str1|str2|str3' -- *strings*txt
like image 178
andersjanmyr Avatar answered Oct 15 '22 22:10

andersjanmyr


A git and grep combined solution:

git grep --files-with-matches "str1"  | xargs grep "str2"
like image 39
cgl Avatar answered Oct 15 '22 23:10

cgl