|
|
Welcome to the Java Forums.
You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:
- have access to post topics
- communicate privately with other members (PM)
- not see advertisements between posts
- have the possibility to earn one of our surprises if you are an active member
- access many other special features that will be introduced later.
Registration is fast, simple and absolutely free so please, join our community today!
If you have any problems with the registration process or your account login, please contact us.
|
|

04-21-2008, 05:41 AM
|
|
Member
|
|
Join Date: Apr 2008
Location: singapore
Posts: 7
|
|
|
[SOLVED] Text Parser
Hi
I am having the following code:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class ReadLogFile {
/**
* @param args
*/
public ReadLogFile(){
}
private List readFile (String inputFile,String content){
List logs = new ArrayList();
try {
FileInputStream fis=new FileInputStream(inputFile);
DataInputStream dis=new DataInputStream(fis);
BufferedReader br=new BufferedReader(new InputStreamReader(dis));
String strLine;
while ((strLine = br.readLine()) != null) {
System.out.println("Line:|" +strLine);
int index;
index = strLine.indexOf(content);
System.out.println("Index:|" +index);
if (index != -1){
System.out.println("log with search string:|" +strLine+"|");
logs.add(strLine);
}else
System.out.println("line does not have serach string");
}
}catch (Exception e){
System.out.println("Exception while reading file:|" +e.getMessage());
}
return logs;
}
private void writeFile(List logs,String outputFile){
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(outputFile,true));
System.out.println("Inside write file method,list size:|" +logs.size());
for(int i=0;i<logs.size();i++){
System.out.println("Inside for loop:|" );
bw.write((String)logs.get(i));
bw.newLine();
System.out.println("file has been written successfully:|" +logs.get(i) );
}
bw.close();
}catch(Exception e){
System.out.println("Exception when writing the file:|" +e.getMessage());
}
}
public static void main(String args[]) throws Exception{
// TODO Auto-generated method stub
String inputFile = null;
String searchString = null;
String outputFile = null;
System.out.print("Enter Input File: ");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
inputFile = br.readLine();
System.out.print("Enter Search String: ");
BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in));
searchString = br1.readLine();
System.out.print("Enter Output File: ");
BufferedReader br2 = new BufferedReader(new InputStreamReader(System.in));
outputFile = br2.readLine();
ReadLogFile logfile = new ReadLogFile();
List log = new ArrayList();
log = logfile.readFile(inputFile,searchString);
System.out.println("Reading log file completed:|");
logfile.writeFile(log,outputFile);
}
}
I need to search a string and write to new file
The string here is date 10-10-2008
Input file
Date Time Server IP Desc
---------------------------------------------------------------
10-10-2008 10:10 Server 1 xx.xx.xx.xx File Server
11-10-2008 20:10 Server 2 xx.xx.xx.xx File Server Linux
12-10-2008 22:10 Server 3 xx.xx.xx.xx File Server Win
13-10-2008 23:10 Server 4 xx.xx.xx.xx File Server aaa
OutPut File
10-10-2008 10:10 Server 1 xx.xx.xx.xx File Server
The current code will write the whole line into a new file when i give a date.
I need more than 2,3 dates to be searched and lines written to new file.
I dont know how to edit
Please help
Thanks in advance.
|
|

04-21-2008, 05:47 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,065
|
|
|
Loop the same process, after processing th first date.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. (Close on September 4, 2008)
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

04-21-2008, 05:58 AM
|
|
Member
|
|
Join Date: Apr 2008
Location: singapore
Posts: 7
|
|
|
can u give me more details in code
|
|

04-21-2008, 06:05 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,065
|
|
|
Can you find the code, where user informations gets in to the application. Look at the main method. Did you find it?
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. (Close on September 4, 2008)
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

04-21-2008, 06:09 AM
|
|
Member
|
|
Join Date: Apr 2008
Location: singapore
Posts: 7
|
|
|
My request is to
give input 2 or three strings to search at a time
Enter Input File: InputFile.txt
Enter Search String:10-10-2008,server2,192.168.1.1
Enter Output File : OutFile.txt
I dont know how to statisfy the conditions more than one in the search string
|
|

04-21-2008, 06:14 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,065
|
|
|
Ah, you want to do that. Ok, how did you done for one search string?
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. (Close on September 4, 2008)
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

04-21-2008, 06:17 AM
|
|
Member
|
|
Join Date: Apr 2008
Location: singapore
Posts: 7
|
|
|
I did not write this code. I am beginner to java.
I downloaded it from web.
Basically i am an unix guy.
trying to help my client for checking daily report of server status.
Eranga, can u help me.
Thanks.
|
|

04-21-2008, 06:37 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,065
|
|
Ok, but it's better to explore something on it. Actually I have to work a lot to fill your requirement. Simply I can do this for you. Change the main method as follows.
public static void main(String args[]) throws Exception{
// TODO Auto-generated method stub
String inputFile = null;
String searchString = null;
String outputFile = null;
do {
System.out.print("Enter Input File: ");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
inputFile = br.readLine();
System.out.print("Enter Search String: ");
BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in));
searchString = br1.readLine();
System.out.print("Enter Output File: ");
BufferedReader br2 = new BufferedReader(new InputStreamReader(System.in));
outputFile = br2.readLine();
ReadLogFile logfile = new ReadLogFile();
List log = new ArrayList();
log = logfile.readFile(inputFile,searchString);
System.out.println("Reading log file completed:|");
logfile.writeFile(log,outputFile);
}while(true);
}
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. (Close on September 4, 2008)
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

04-21-2008, 07:44 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,065
|
|
Here what you want, do it with busy works. Test and let me know.
public static void main(String args[]) throws Exception{
// TODO Auto-generated method stub
String inputFile = null;
String searchString = null;
String outputFile = null;
System.out.print("Enter Input File: ");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
inputFile = br.readLine();
System.out.print("Enter Search String: ");
BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in));
searchString = br1.readLine();
System.out.print("Enter Output File: ");
BufferedReader br2 = new BufferedReader(new InputStreamReader(System.in));
outputFile = br2.readLine();
ReadLogFile logfile = new ReadLogFile();
String[] nn = searchString.split(",");
for(int i = 0; i < nn.length; i++){
List log = new ArrayList();
log = logfile.readFile(inputFile,nn[i]);
logfile.writeFile(log,outputFile);
}
System.out.println("Reading log file completed:|");
}
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. (Close on September 4, 2008)
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

04-21-2008, 11:32 AM
|
|
Member
|
|
Join Date: Apr 2008
Location: singapore
Posts: 7
|
|
Hi Eranga,
I could`n get output wat i really needed.
I have already escalated to Application Support.
Anyway thanks for your cooperation.
Please sign my guestbook in www(dot)karthik(dot)sg
thanks
. 
|
|

04-21-2008, 01:11 PM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,065
|
|
Originally Posted by karthik7974
I could`n get output wat i really needed.
What you mean here, as far as I get you the last post mine contain the correct code. Isn't?
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. (Close on September 4, 2008)
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

04-21-2008, 01:18 PM
|
 |
Moderator
|
|
Join Date: Aug 2007
Location: London, UK
Posts: 239
|
|
You need something like this:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.io.Writer;
public class StringFind {
public static String path;
public static String FindMe;
public String strLine;
public void openFile(){
String newLine = System.getProperty("line.separator");
try {
Writer output = new BufferedWriter(new FileWriter("output.txt"));
FileInputStream in = new FileInputStream(path);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
while ((strLine = br.readLine()) != null){
if (strLine.contains(FindMe)) {
output.write(strLine);
output.write(newLine);
}
}
output.close();
System.out.println("Completed. Output file generated.");
}catch(Exception e) {
System.out.println("OUCH! I fell over.");
System.exit(0);
}
}
public static void main(String[] args) {
StringFind sf = new StringFind();
if (args.length < 2){
System.out.println("ERROR! No parameters sent");
System.exit(0);
}
//path = "myFile.txt";
//FindMe = "10-10-2008";
path = args[0];
FindMe = args[1];
sf.openFile();
}
}
This code takes 2 parameters. The first is the file path eg: myfile.txt and the second is the string you are looking for.
As it reads the file it will write all the lines that match FindMe to the output.txt file.
__________________
Did this post help you? Please To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. me! To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Last edited by DonCash : 04-21-2008 at 05:23 PM.
Reason: Making a few corrections...
|
|

04-21-2008, 01:55 PM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,065
|
|
Originally Posted by DonCash
This code takes 2 parameters. The first is the file path eg: myfile.txt and the second is the string you are looking for.
As it reads the file it will write all the lines that match FindMe to the output.txt file.
Yep . But I don't know he can manage this code!
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. (Close on September 4, 2008)
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

04-21-2008, 01:58 PM
|
 |
Moderator
|
|
Join Date: Aug 2007
Location: London, UK
Posts: 239
|
|
|
We will see!! We can help with any problems he is having with it.. I am coding something similar myself right now.
__________________
Did this post help you? Please To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. me! To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

04-21-2008, 02:04 PM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,065
|
|
Ok we will see pal 
By the way did you try my second answer on the post number 9. For me it's working fine. Their I can give multiple search strings for the file searching. 
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. (Close on September 4, 2008)
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

04-21-2008, 02:13 PM
|
 |
Moderator
|
|
Join Date: Aug 2007
Location: London, UK
Posts: 239
|
|
|
No, I couldn't actually get it to work.
I guess its because it contains 2 classes and you only posted the main one?
What is the content of the ReadLogFile class?
I think this would of been quite confusing to fix for someone who doesn't know Java.
__________________
Did this post help you? Please To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. me! To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

04-21-2008, 05:21 PM
|
|
Member
|
|
Join Date: Apr 2008
Location: singapore
Posts: 7
|
|
|
Hi Don,
Thanks for your help,
I tried your code too. Initially got an error. u did not declare deleteMe.
I declared and it was compiled with no errors.
But can see only empty output.txt
i received OUCH! I Fell Over.
I think my request in this forum is not clear, maybe.
I will clear it now.
The code i posted is working fine. but works for one string at a time.
But my request is to send the line to output file only if the below condition is true
Enter Log File : Input.txt
Enter String : 10/10/2008,server1,xxx.xxx.xxx.xxx
Enter Output File : Output.txt
only if 10/10/2008,server1,xxx.xxx.xxx.xxx (ip address) these 3 strings are in a line then that line has to be send to output.
but all your replies satisfies either one of the 3 string in a line was sent to output file
I want an output file in which all the 3 strings are in the same line.
Can you help.
Hi Eranga,
I am not familiar with java basics, but a little knowledge with scripting.
I own a forum too www(dot)karthik(dot)sg/MyForum/
but not enough time to develop my programming skills
R U OK to teach me online.
I am ready to help your forum with any pace, like answering unix+java related questions and even something else.
Hi Webmaster,
Please edit any words exceed your terms & conditions. Sorry if so. I really mean to express my good will to all.
Thanks to all.
|
|

04-21-2008, 05:27 PM
|
 |
Moderator
|
|
Join Date: Aug 2007
Location: London, UK
Posts: 239
|
|
|
Hey,
Sorry about that. I edited the code after I posted it and forgot to change deleteMe to FindMe. I have updated it again now so it should work fine.
__________________
Did this post help you? Please To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. me! To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Last edited by DonCash : 04-21-2008 at 05:38 PM.
|
|

04-21-2008, 05:40 PM
|
 |
Moderator
|
|
Join Date: Aug 2007
Location: London, UK | | | |