Git:git grepコマンドで複数パターンでand, or検索する方法

スポンサーリンク

git grepコマンドで複数パターンでand, or検索する方法

git grepコマンドで複数パターンでand, or検索するには--and, --orオプションを指定します。

 

オプションの説明

 -e
     The next parameter is the pattern. This option has to be used for patterns starting with - and should be used in scripts passing user input to grep.
     Multiple patterns are combined by or.

 --and, --or, --not, ( ... )
     Specify how multiple patterns are combined using Boolean expressions.  --or is the default operator.  --and has higher precedence than --or.  -e has to
     be used for all patterns.

 

--andオプションを指定した場合(-eオプションはパターン毎に付与)

$ git grep -e "#define" --and -e MAX | head
Documentation/git-grep.txt:`git grep -e '#define' --and \( -e MAX_PATH -e PATH_MAX \)`::
Documentation/git-grep.txt:     Looks for a line that has `#define` and either `MAX_PATH` or
abspath.c:#define MAXSYMLINKS 32
archive-tar.c:#define USTAR_MAX_SIZE ULONG_MAX
archive-tar.c:#define USTAR_MAX_SIZE 077777777777UL
archive-tar.c:#define USTAR_MAX_MTIME TIME_MAX
archive-tar.c:#define USTAR_MAX_MTIME 077777777777ULL
blame.c:#define MAXSG 16
bloom.h:#define DEFAULT_BLOOM_MAX_CHANGES 512
bloom.h:#define DEFAULT_BLOOM_FILTER_SETTINGS { 1, 7, 10, DEFAULT_BLOOM_MAX_CHANGES }

$ git grep -e "#define" --and -e MAX --and -e MIN | head
compat/nedmalloc/malloc.c.h:#define MAX_REQUEST         ((-MIN_CHUNK_SIZE) << 2)
compat/nedmalloc/malloc.c.h:#define MAX_SMALL_SIZE    (MIN_LARGE_SIZE - SIZE_T_ONE)
compat/vcbuild/include/unistd.h:#define INTMAX_MIN  _I64_MIN
xdiff/xdiffi.c:#define XDL_MAX_COST_MIN 256

 

--orオプションを指定した場合(デフォルトはor検索なので--orはなくてもよい)

$ git grep -e "#define" --or -e MAX | head
.clang-format:# #define A   \
.clang-format:#   git grep -h '^#define [^[:space:]]*for_each[^[:space:]]*(' \
.clang-format:#   | sed "s,^#define \([^[:space:]]*for_each[^[:space:]]*\)(.*$,  - '\1'," \
Documentation/RelNotes/1.5.6.4.txt:  with small PATH_MAX value in a repository that has contents with
Documentation/RelNotes/1.7.12.1.txt:   descriptors we can open around sysconf(_SC_OPEN_MAX) and
Documentation/RelNotes/1.8.2.1.txt: * Some sources failed to compile on systems that lack NI_MAXHOST in
Documentation/RelNotes/1.8.3.3.txt: * Mac OS X does not like to write(2) more than INT_MAX number of
Documentation/RelNotes/1.8.3.txt: * Some sources failed to compile on systems that lack NI_MAXHOST in
Documentation/RelNotes/1.8.4.txt: * Mac OS X does not like to write(2) more than INT_MAX number of
Documentation/RelNotes/2.14.0.txt: * We started using "%" PRItime, imitating "%" PRIuMAX and friends, as