在grep
的手册中,我们有-o
打印--only-matching
模式。说echo foobar | grep foo
将返回foobar
,但是将-o
添加到grep
只会得到foo
。
许多grep
选项(例如-P
,-c
等)可以与git
结合使用,以搜索Git索引中的所有文件。但是,git grep -o PAT
会触发error: unknown switch
o'`。
如何为Git索引中的每个文件仅打印匹配的字符串?即“ git grep -o PAT
”
我的审判:
for f in `git ls-files`; do grep -o PAT $f; done
答案 0 :(得分:4)
git grep 2.18没有选项-o|--only-matching
,git grep 2.19没有。如果您的版本低于2.19,则不能使用该选项。要使用它,您必须升级。