问题描述
在grep
的手册中,我们有-o
可以打印--only-matching
模式.假设echo foobar | grep foo
将返回foobar
,但是将-o
添加到grep
只会得到foo
.
In the manual for grep
, we have -o
to print --only-matching
patterns. Say echo foobar | grep foo
would return foobar
, but adding -o
to grep
would give only foo
.
许多grep
选项(例如-P
,-c
等)可以与git
结合使用,以搜索Git索引中的所有文件.但是,git grep -o PAT
会触发error: unknown switch
o'`.
Many grep
options like -P
, -c
, etc, can be used in conjunction with git
to search through all files in the Git index. However, git grep -o PAT
triggers an error: unknown switch
o'`.
如何为Git索引中的每个文件仅打印匹配的字符串?即"git grep -o PAT
"
How can I print only matched string for each file in the Git index? i.e. "git grep -o PAT
"
我的审判:
for f in `git ls-files`; do grep -o PAT $f; done
推荐答案
git grep 2.18 没有选项-o|--only-matching
, git grep 2.19 具有.如果您的版本低于2.19,则不能使用该选项.要使用它,您必须升级.
git grep 2.18 doesn't have the option -o|--only-matching
, git grep 2.19 has. If your version is lower than 2.19 you cannot use the option. To use it you have to upgrade.
这篇关于Git grep只打印匹配的图案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!