本文介绍了Git grep只打印匹配的图案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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 switcho'`.

如何为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只打印匹配的图案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 04:08