Git grep只打印匹配的图案 [英] Git grep print only matching pattern

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆