My intention: Search trough source code and find keywords of interest. This is done to automate a small portion of codereview to find obvious programming errors like hardcoded keys and passwords.
I currently have the following grep command to search trough code for certain words:
while read p; do
echo "FOUND: ${p}"
grep -riIn -A 5 -B 5 ${p} "${SEARCHPATH}"
done < "${SEARCHWORDS}"
SEARCHWORDS
is actually a file location with a list containing searchwords.
SEARCHPATH
is the folder which grep should search in.
The output it generates is as following:
xo.java-33- default:
xo.java-34- return str;
xo.java-35- case -4501:
xo.java-36- return "Internal error";
xo.java-37- case -4502:
xo.java:38: return "Activation password too long. Limited to 512 characters.";
xo.java-39- case -4503:
xo.java-40- return "CHS key null or empty. Must be a 32 hexadecimal string.";
xo.java-41- case -4504:
xo.java-42- return "Incorrect CHS key length. Must be a 32 hexadecimal string.";
xo.java-43- case -4505:
As you can see, it also gives the lines above and below, this to give me some context and see if it is a false positive. But I would like to have the following output:
Found "password" in file "xo.java":
xo.java-33- default:
xo.java-34- return str;
xo.java-35- case -4501:
xo.java-36- return "Internal error";
xo.java-37- case -4502:
xo.java:38: return "Activation password too long. Limited to 512 characters.";
xo.java-39- case -4503:
xo.java-40- return "CHS key null or empty. Must be a 32 hexadecimal string.";
xo.java-41- case -4504:
xo.java-42- return "Incorrect CHS key length. Must be a 32 hexadecimal string.";
xo.java-43- case -4505:
I want the found search word on top of it, so all instances are kind of grouped together with their found keyword.
If you have suggestions on other tools, feel free to share them. I tried the command ack
, but I couldn't achieve the result as I describe here.