Help me to find files in a shell script with any matching pattern

Hi friends.. I have many dirs in my working directory. Every dir have thousands of files (.jsp, .java, .xml..., etc). So I am working with an script to find every file recursively within those directories and subdirectories ending with .jsp or .java which contains inside of it, the the pattern "System.out.println", but at the same time it has to ingnore in the output those files which have same pattern, but with comment out lines: like this for example ( // System.out.println ). It has been difficult to me to ingnore those files with // System.out.println. files have tabs in some cases but in others not, I mean the format of files are diferent each others.

Here's my code.

for file in `cat /tmp/found_projects`
do
     file2=`find  $file \( -name "*.java" -o -name "*.jsp" \) -type f`
     _file=`grep -l System.out.println $file2 | grep -v testcases`
      grep System.out.println "$_file" | sed 's/^[ \t]*//' | grep "^System.out.println" >/dev/null 2>&1
     if [ $? -eq 0 ]; then
       echo "$_file ---> Found"
     else
       echo "$_file ---> Not Found"
     fi
done

--
The result are not the expected..

found_projects --> is the list of dirs i am using the search inside of them.
sed 's/^[ \t]*//' --> for deleting all tabs and then check what files have lines starting with System.out.println

In conclusion, what I need is:
in conclusion is, get all the files that have the pattern System.out.println and ignore those on whose pattern is coment out, for example //System.out.println

useless use of cat and useless use of backticks, don't do that.

You also seem to be doing three greps when you only need one or two. And there's no need for the sed at all, you can put that bit of the regex right inside grep itself.

while read DIR
do
        find "$DIR" \( -name "*.java" -o -name "*.jsp" \) -type f |
        while read FILE
        do

                if grep '^[ \t]*System\.out\.println' "$FILE" | grep -v testcases > /dev/null
                then
                        echo "$FILE"
                fi
        done

done < /tmp/found_projects > files_with_println

Hi Corona688,

Thank you for your sugestion.
Your script worked in some cases, but in others not because it ignores the files with tabs before System.out.println lines although the line have not comment out

---------- Post updated at 08:54 AM ---------- Previous update was at 07:13 AM ----------

Thank you so much..., I did a little modifcation to your script and now is wokring as expected.

This was the test:

if grep -v testcases "$FILE" | perl -pe 's/[ \t]+//' | grep '^System\.out\.println'

Thank you very much !

You have an extremely strange version of 'grep' to be ignoring the expression I gave it, "[ \t]*System\.out\.println" , which should work fine by itself -- see the tab -- and not need to run the entirety of perl for every single file which is processed... Try egrep.