List file's name and search string's Line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting List file's name and search string's Line
# 1  
Old 09-20-2011
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:
Code:
/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 :
Code:
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:
Code:
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 ??
# 2  
Old 09-20-2011
aha! You want the output in your defined format, not the infos grepped.

Code:
filename
codes...


you could try this:
Code:
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.

Last edited by sk1418; 09-20-2011 at 11:54 AM..
# 3  
Old 09-20-2011
Code:
 
 
---- 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

# 4  
Old 09-20-2011
Quote:
Originally Posted by sk1418
Code:
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.
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..
Code:
command]# cat All_JavaFiles | grep -A3 -B2 "Sysout" | and print respective-filename

# 5  
Old 09-20-2011
deleted again!

--ahamed

Last edited by ahamed101; 09-20-2011 at 01:40 PM..
# 6  
Old 09-20-2011
does this work? run it under your src root.

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

This User Gave Thanks to sk1418 For This Post:
# 7  
Old 09-20-2011
is it possible

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

Code:
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:
Code:
..
..
..
/home/me/project/convertBoolean.java
    //use toString method of Boolean class to convert it into String
    String str = blnObj.toString();
    System.out.println(str);
  }
}
..
..
..

Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Need help with how to search a file for a variable string and delete that line

Hi, I have a working script. It does what I am intending it to but a bit confused whether the sed part is supposed to be working or not. Further down is the script with the sed part that should have been working but not and the grep -v part which is the workaround that I am using at the... (10 Replies)
Discussion started by: newbie_01
10 Replies

2. Shell Programming and Scripting

Search a string in a file and change another in the line

I did it myself (0 Replies)
Discussion started by: apenkov
0 Replies

3. UNIX for Dummies Questions & Answers

Search for a string,delete the line and replace with new string in a file

Hi Everyone, I have a requirement in ksh where i have a set of files in a directory. I need to search each and every file if a particular string is present in the file, delete that line and replace that line with another string expression in the same file. I am very new to unix. Kindly help... (10 Replies)
Discussion started by: Pradhikshan
10 Replies

4. Shell Programming and Scripting

Search string within a file and list common words from the line having the search string

Hi, Need your help for this scripting issue I have. I am not really good at this, so seeking your help. I have a file looking similar to this: Hello, i am human and name=ABCD. How are you? Hello, i am human and name=PQRS. I am good. Hello, i am human and name=ABCD. Good bye. Hello, i... (12 Replies)
Discussion started by: royzlife
12 Replies

5. Shell Programming and Scripting

Search a string in a text file and add another string at the end of line

Dear All I am having a text file which is having more than 200 lines. EX: 001010122 12000 BIB 12000 11200 1200003 001010122 2000 AND 12000 11200 1200003 001010122 12000 KVB 12000 11200 1200003 In the above file i want to search for string KVB... (5 Replies)
Discussion started by: suryanarayana
5 Replies

6. Shell Programming and Scripting

Search a string in a text file and add another string at the particular position of a line

I am having a text file which is having more than 200 lines. EX: 001010122 12000 BIB 12000 11200 1200003 001010122 2000 AND 12000 11200 1200003 001010122 12000 KVB 12000 11200 1200003 In the above file i want to search for string KVB and add/replace... (1 Reply)
Discussion started by: suryanarayana
1 Replies

7. Shell Programming and Scripting

Search several string and convert into a single line for each search string using awk command AIX?.

I need to search the file using strings "Request Type" , " Request Method" , "Response Type" and by using result set find the xml tags and convert into a single line?. below are the scenarios. Cat test Nov 10, 2012 5:17:53 AM INFO: Request Type Line 1.... (5 Replies)
Discussion started by: laknar
5 Replies

8. Shell Programming and Scripting

search a string in a particular column of file and return the line number of the line

Hi All, Can you please guide me to search a string in a particular column of file and return the line number of the line where it was found using awk. As an example : abc.txt 7000,john,2,1,0,1,6 7001,elen,2,2,0,1,7 7002,sami,2,3,0,1,6 7003,mike,1,4,0,2,1 8001,nike,1,5,0,1,8... (3 Replies)
Discussion started by: arunshankar.c
3 Replies

9. UNIX for Dummies Questions & Answers

Search String in File List

Hi, I have a list of files which can be identified as abcd*.xml where * represent multiple timestamps for multiple files. I need to do following checks 1. Find number of files (which we get by doing following) FILECOUNT=`ls -ltr /dir/abcd*.xml | wc -l` 2. If check 1 gives '1' as... (2 Replies)
Discussion started by: dsrookie
2 Replies

10. Shell Programming and Scripting

search string in a file and retrieve 10 lines including string line

Hi Guys, I am trying to write a perl script to search a string "Name" in the file "FILE" and also want to create a new file and push the searched string Name line along with 10 lines following the same. can anyone of you please let me know how to go about it ? (8 Replies)
Discussion started by: sukrish
8 Replies
Login or Register to Ask a Question

Featured Tech Videos