Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





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.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-21-2008, 05:41 AM
Member
 
Join Date: Apr 2008
Location: singapore
Posts: 7
karthik7974 is on a distinguished road
Send a message via Yahoo to karthik7974
[SOLVED] Text Parser
Hi
I am having the following code:

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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 04-21-2008, 05:47 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,065
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 04-21-2008, 05:58 AM
Member
 
Join Date: Apr 2008
Location: singapore
Posts: 7
karthik7974 is on a distinguished road
Send a message via Yahoo to karthik7974
can u give me more details in code
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 04-21-2008, 06:05 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,065
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 04-21-2008, 06:09 AM
Member
 
Join Date: Apr 2008
Location: singapore
Posts: 7
karthik7974 is on a distinguished road
Send a message via Yahoo to karthik7974
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
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 04-21-2008, 06:14 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,065
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 04-21-2008, 06:17 AM
Member
 
Join Date: Apr 2008
Location: singapore
Posts: 7
karthik7974 is on a distinguished road
Send a message via Yahoo to karthik7974
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.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 04-21-2008, 06:37 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,065
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.

Code:
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.
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 04-21-2008, 07:44 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,065
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Here what you want, do it with busy works. Test and let me know.

Code:
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.
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 04-21-2008, 11:32 AM
Member
 
Join Date: Apr 2008
Location: singapore
Posts: 7
karthik7974 is on a distinguished road
Send a message via Yahoo to karthik7974
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

.
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 04-21-2008, 01:11 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,065
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Quote:
Originally Posted by karthik7974 View Post

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.
Bookmark Post in Technorati
Reply With Quote
  #12 (permalink)  
Old 04-21-2008, 01:18 PM
DonCash's Avatar
Moderator
 
Join Date: Aug 2007
Location: London, UK
Posts: 239
DonCash will become famous soon enoughDonCash will become famous soon enough
You need something like this:

Code:
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...
Bookmark Post in Technorati
Reply With Quote
  #13 (permalink)  
Old 04-21-2008, 01:55 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,065
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Quote:
Originally Posted by DonCash View Post

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.
Bookmark Post in Technorati
Reply With Quote
  #14 (permalink)  
Old 04-21-2008, 01:58 PM
DonCash's Avatar
Moderator
 
Join Date: Aug 2007
Location: London, UK
Posts: 239
DonCash will become famous soon enoughDonCash will become famous soon enough
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.
Bookmark Post in Technorati
Reply With Quote
  #15 (permalink)  
Old 04-21-2008, 02:04 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,065
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.
Bookmark Post in Technorati
Reply With Quote
  #16 (permalink)  
Old 04-21-2008, 02:13 PM
DonCash's Avatar
Moderator
 
Join Date: Aug 2007
Location: London, UK
Posts: 239
DonCash will become famous soon enoughDonCash will become famous soon enough
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.
Bookmark Post in Technorati
Reply With Quote
  #17 (permalink)  
Old 04-21-2008, 05:21 PM
Member
 
Join Date: Apr 2008
Location: singapore
Posts: 7
karthik7974 is on a distinguished road
Send a message via Yahoo to karthik7974
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.
Bookmark Post in Technorati
Reply With Quote
  #18 (permalink)  
Old 04-21-2008, 05:27 PM
DonCash's Avatar
Moderator
 
Join Date: Aug 2007
Location: London, UK
Posts: 239
DonCash will become famous soon enoughDonCash will become famous soon enough
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.
Bookmark Post in Technorati
Reply With Quote
  #19 (permalink)  
Old 04-21-2008, 05:40 PM
DonCash's Avatar
Moderator
 
Join Date: Aug 2007
Location: London, UK