我们的源代码中遍布错误代码。使用grep可以很容易地找到它们,但是我想要一个find_code
可以执行的bash函数(例如find_code ####
),它将提供以下内容的输出:
/home/user/path/to/source.c
85 imagine this is code
86 this is more code
87 {
88 nicely indented
89 errorCode = 1111
90 that's the line that matched!
91 ok this block is ending
92 }
93 }
这是我目前拥有的:
find_code()
{
# "= " included to avoid matching unrelated number series
# SRCDIR is environment variable, parent dir of all of projects
FILENAME= grep -r "= ${1}" ${SRCDIR}
echo ${FILENAME}
grep -A5 -B5 -r "= ${1}" ${SRCDIR} | sed -e 's/.*\.c\[-:]//g'
}
问题:
1)这不提供行号
2)仅匹配.c源文件。我无法通过s来匹配.c,.cs,.cpp和其他源文件。但是,我们确实使用C,因此只需匹配-或:(grep在每行代码之前附加到文件名的字符)匹配object->pointers
并弄乱所有内容。
MATCH="= ${1}"
。我还添加--include=*.c --include=*.cpp --include=*.java --include=*.cs
了将搜索限制为源文件的功能。谢谢!