git-diff 报告包含更改的直接函数名称,如下所示:
$ git diff
diff --git a/apps/cli/elmo.py b/apps/cli/elmo.py
index ac056e9..1b7c1d0 100644
--- a/myfile.c
+++ b/myfile.c
@@ -100,12 +106,20 @@ int myioctl(unsigned int cmd, int size, int direction, unsigned long arg
rc = myfunc1(ioargp);
break;
- case IOCTL_1:
- rc = myfunc1(ioargp);
+ case IOCTL_2:
+ rc = myfunc2(arg);
break;
有没有办法让 grep 报告匹配的函数上下文,类似于 git-diff (即,@@ int myioctl(...) 在上述情况下)?或者是否有一些 grep 的替代工具,其行为类似于 grep 但也会报告函数上下文?
我经常对源代码进行 grep,了解包含匹配行的函数很有帮助,而无需打开文件并查找特定模式或行号。
谢谢!
最佳答案
这并不完全可行,因为我认为您无法使用正则表达式表示所有形式的函数语句。你需要一个语法。
反正
所以,你有文件名
现在您需要找出匹配发生的行号。
grep -n "pattern" MYFILE | sed -r 's/([0-9]*):.*/\1/'
您想知道上面印有哪一行的函数
grep -n "pattern" MYFILE | sed -r 's/([0-9]*):.*/\1/' | while read lineNo
do
head -n $lineNo MYFILE | tac | egrep -m 1 "[[:alnum:]]+[[:space:]]+[[:alnum:]](.*)[[:space:]]*{"
done
这将为您提供包含发生匹配行的函数的名称(希望如此)。
(我正在使用 egrep 所以我可以使用 '+' 运算符。)
请注意,此模式与函数声明相匹配,如下所示:
<function-type> <function-identifier> ( <things> ) {
您将需要对其进行扩展,以便它可以表示更多形式的函数语句。
关于function - grep 报告在上下文中包含函数名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12823013/