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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help me to find files in a shell script with any matching pattern
# 1  
Old 01-25-2012
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.

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

Last edited by methyl; 01-25-2012 at 06:48 PM.. Reason: please use code tags.
# 2  
Old 01-25-2012
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.

Code:
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

# 3  
Old 01-26-2012
Help me to find files in a shll script with any matching pattern

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:
Code:
if grep -v testcases "$FILE" | perl -pe 's/[ \t]+//' | grep '^System\.out\.println'

Thank you very much !
Moderator's Comments:
Mod Comment Please use next time code tags for your code and data

Last edited by vbe; 01-26-2012 at 10:07 AM..
# 4  
Old 01-26-2012
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

find Search - Find files not matching a pattern

Hello all, this is my first and probably not my last question around here. I do hope you can help or at least point me in the right direction. My question is as follows, I need to find files and possible folders which are not owner = AAA group = BBB with a said location and all sub folders ... (7 Replies)
Discussion started by: kilobyter
7 Replies

2. Shell Programming and Scripting

how to find files matching a pattern and zip

Hi, I want to find all directories matching given pattern in current directory and zip those files. I am trying to do somethign like this. But it is not working. for FNAME in $(find . -type d | grep './\{2\}-\{2\}$'); do zip -r MatchedFiles.zip $FNAME rm -fr $FNAME done ... (4 Replies)
Discussion started by: james423
4 Replies

3. Homework & Coursework Questions

shell script that can create, monitor the log files and report the issues for matching pattern

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Write an automated shell program(s) that can create, monitor the log files and report the issues for matching... (0 Replies)
Discussion started by: itian2010
0 Replies

4. Shell Programming and Scripting

To find files by matching a pattern in file name

Hi all, I have to check whether certain files exist using a if statement. I have to check this by matching a pattern of filename: e.g. if ] This statement should be "true" if any files like test.dat11, test.dat22 etc are present in the source dir. However, this statement is checking only... (2 Replies)
Discussion started by: sweety123
2 Replies

5. Shell Programming and Scripting

Pattern matching in shell script

Hi, I am using following command to extract string from a file. String will be after last / (slash). awk -F\ / '{print $NF}' $FILE but while appending the output in file in script, it dosent work. File created but of zero size... can anyone please help `awk -F\\\/ '{print $NF}' $FILE` >... (3 Replies)
Discussion started by: Deei
3 Replies

6. Shell Programming and Scripting

shell script pattern matching

Hi, I need to create a shell script through which i need to populate email addresses in email columns of database table in mysql. Let say if email contains yahoo, hotmail, gtalk than email addresses need to move in their respective columns. # !/bin/sh yim="example@yahoo.com"... (3 Replies)
Discussion started by: mirfan
3 Replies

7. UNIX for Dummies Questions & Answers

find files NOT matching name pattern

Hi, I have following files in my directory: /TESTDONTDEL> ls -alt total 14 drwxr-xr-x 2 oracle dba 1024 May 15 06:30 . -rw-r--r-- 1 oracle dba 40 May 15 06:30 exception.txt -rw-r--r-- 1 oracle dba 19 May 15 06:22 ful_1234_test1.txt -rw-r--r-- 1... (2 Replies)
Discussion started by: sagarparadkar
2 Replies

8. UNIX for Dummies Questions & Answers

Find files matching a pattern

Hi, I am writing a BASH shell script. I would like to count all the files in the CURRENT directory matching a specific pattern. Could someone suggest the best/simplest way to do this. I have thought of these solutions (for simplicity the pattern is all files starting with A): ls -1 *A | wc -l... (5 Replies)
Discussion started by: msb65
5 Replies

9. Shell Programming and Scripting

Pattern matching in a shell script?

I'm looking for a way to match a particular string in another string and if a match is found execute some command. I found the case statement can be used like this; case word in ) command ;; ] ... esac If my string to find is say "foo" in the string $mystring... (1 Reply)
Discussion started by: paulobrad
1 Replies

10. Shell Programming and Scripting

Script to find file name for non matching pattern

Hi, I want to list only the file names which do not contain a specific keyword or search string. OS: Solaris Also is there any way ; through the same script I can save the output of search to a CSV (comma seperated) so that the file can be used for inventory purpose. Any assistance will... (5 Replies)
Discussion started by: sujoy101
5 Replies
Login or Register to Ask a Question