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?
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.
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.
The git grep version will only search in files tracked by git, whereas the grep version will search everything in the directory.
I find it easiest to use extended regular expressions -E
and |
(or).
git grep -E 'str1|str2|str3' -- *strings*txt
A git and grep combined solution:
git grep --files-with-matches "str1" | xargs grep "str2"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With