什么grep命令将在其输出中包含当前函数名称? [英] What grep command will include the current function name in its output?

查看:136
本文介绍了什么grep命令将在其输出中包含当前函数名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用 -p 选项运行 diff ,这样输出将包含函数的名称,每个变化发生。对于 grep 有没有类似的选项?如果不是,我可以使用其他命令吗?



代替 -B 显示固定数量的上下文在比赛之前立即进行的比赛中,我希望比赛前面只有一行最近的功能签名,但是在文件中有很多线条。如果我正在查找的选项是 -p ,则输出可能如下所示:

 
$ cat foo.c
int func1(int x,int y)
{
return x + y;
}
int func2(int x,int y,int z)
{
int tmp = x + y;
tmp * = z;
return tmp;


$ grep -p -n -e'return'foo.c
1-int func1(int x,int y)
3:return x + y;
-
5-int func2(int x,int y,int z)
9:return tmp;


解决方案

GNU grep中没有这样的函数,尽管它已经>讨论

然而,如果你的代码在 git 的控制下, git grep 有一个选项 -p 就可以做到这一点。


I run diff with the -p option so the output will include the name of the function where each change occurred. Is there an analogous option for grep? If not, what other command could I use instead?

Instead of -B to show a fixed number of context lines that immediately precede a match, I'd like for the match to be preceded by just one line with the most recent function signature, however many lines back it was in the file. If the option I'm looking for were -p, output might look like this, for example:

$ cat foo.c
int func1(int x, int y)
{
  return x + y;
}
int func2(int x, int y, int z)
{
  int tmp = x + y;
  tmp *= z;
  return tmp;
}

$ grep -p -n -e 'return' foo.c
1-int func1(int x, int y)
3:  return x + y;
--
5-int func2(int x, int y, int z)
9:  return tmp;

解决方案

There is no such function in GNU grep, although it has been discussed in the past.

However if your code is under git's control, git grep has an option -p that will do that.

这篇关于什么grep命令将在其输出中包含当前函数名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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