Our source code has error codes scattered throughout. Finding them is easy with grep, but I'd like a bash function find_code
that I can execute (eg. find_code ####
) which will provide output along these lines:
/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 }
Here is what I have currently:
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'
}
Problems:
1) This doesn't provide line numbers
2) it only matches .c source files. I'm having trouble getting sed to match .c, .cs, .cpp, and other source files. We do use C, though, so simply matching - or : (the characters that grep appends to the filename before each line of code) matches object->pointers
and messes everything up.