List file's name and search string's Line

Hi all,

My need is:
To list out all the java / jsp files having System.out.println[before 2 lines & after 3 lines] in my project.

I need the output as:

/home/me/project/file1.java
somejavacode
somejavacode
System.out.println("something");
somejavacode
somejavacode
somejavacode

/home/me/project/file2.java
somejavacode
somejavacode
System.out.println("something");
somejavacode
somejavacode
somejavacode

/home/me/project/file1.jsp
somejavacode
somejavacode
System.out.println("something");
somejavacode
somejavacode
somejavacode

etc..

What i tried is :

find /home/me/project -type f | grep -iE ".java|.jsp" | grep -vE ".svn|svn" |xargs awk -F"System.out.println" '{print FILENAME}' | uniq
--> This will list the file names (containing SysOut) Alone

Next i tried:

find /home/me/project \( -name "*jsp" -o -name "*java" \) -type f -exec echo -e "\n" '{}' \; -exec grep -A3 -B3 -H "System.out.println" '{}' \; -print | grep -vE ".svn|svn"  | uniq

--> Didn't get what i need.. :(

Any other way for this ??

aha! You want the output in your defined format, not the infos grepped.

filename
codes...

you could try this:

grep -l "System.out.println" **/*.java > tmp.txt
this will list all filenames
then
grep -B2 -A3 "System.out.println" **/*.java | awk 'NR==FNR{. set the array..}NR>FNR{..do..filter..}' tmp.txt -

well I am a bit lazy, didn't provide the full code. but I think you've got it, right.

 
 
---- loop through each file which does have "System.out.println" here I am assuming file_name is the one of them
 
SCRIPTS>awk 'c&&c--{print};/System/{c=2;print "\n"q"\n"p"\n"$0}{q=p;p=$0}'  file_name
 

oh.. i am sorry. I couldn't make it..

Can you please do it for me..?

And panyam,
awk syntax error..
And also what file name..?
I did't understand it.

I just need the output for..

command]# cat All_JavaFiles | grep -A3 -B2 "Sysout" | and print respective-filename

deleted again!

--ahamed

does this work? run it under your src root.

 grep  -r -B2 -A3 "System.out.println" java|awk -F'-|:' '{if($1&&x!=$1){print $1;x=$1;}  print $2}'
1 Like

ahamed,

i have a Java project with more than 5000 java/jsp files.

In about all the java/jsp files there is System.out.println.

I need to comment all the //System.out.println line from all the java files.
But need to see which are all the files before commenting. --> This [commenting] I will do later

Now my need is to list all the java files containing system.out.println:
and the output i need is :

/home/me/project/file1.java [system.out.println containing file name]
somejavacode [second line before system.out.println]
somejavacode [first line before system.out.println]
System.out.println("something");
somejavacode [first line after system.out.println]
somejavacode [second line after system.out.println]
somejavacode [third line after system.out.println]

/home/me/project/file156.java [system.out.println containing file name]
somejavacode [second line before system.out.println]
somejavacode [first line before system.out.println]
System.out.println("something");
somejavacode [first line after system.out.println]
somejavacode [second line after system.out.println]
somejavacode [third line after system.out.println]

/home/me/project/file871.java [system.out.println containing file name]
somejavacode [second line before system.out.println]
somejavacode [first line before system.out.println]
System.out.println("something");
somejavacode [first line after system.out.println]
somejavacode [second line after system.out.println]
somejavacode [third line after system.out.println]
etc..
Here is an Example java file convertBoolean.java
/*
  Convert java Boolean object to String object Example
  This example shows how to convert java Boolean object into String object.
*/
 
public class JavaBooleanToStringExample {
 
  public static void main(String[] args) {
    //construct Boolean object
    Boolean blnObj = new Boolean("true");
 
    //use toString method of Boolean class to convert it into String
    String str = blnObj.toString();
    System.out.println(str);
  }
}
 

I need the Output of our command as:

..
..
..
/home/me/project/convertBoolean.java
    //use toString method of Boolean class to convert it into String
    String str = blnObj.toString();
    System.out.println(str);
  }
}
..
..
..

:wall:

@linuxadmin
what kind of output the command in #6 giving u?
here it looks quite similar with your expectation. :smiley:

Yes sk1418,
You rocks.

I just changed it as..

grep -r -B2 -A3 "System.out.println" `find /home/me/project \( -name "*jsp" -o -name "*java" \) -type f | uniq` |awk -F'-|:' '{if($1&&x!=$1){print $1;x=$1;}  print $2}'

It works fine.

Thanks again.