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
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);
}
}
..
..
..