Page 1 of 2 12 LastLast
Results 1 to 20 of 21
  1. #1
    karthik7974 is offline Member
    Join Date
    Apr 2008
    Location
    singapore
    Posts
    7
    Rep Power
    0

    Question [SOLVED] Text Parser

    Hi
    I am having the following code:

    Java 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.

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  3. #3
    karthik7974 is offline Member
    Join Date
    Apr 2008
    Location
    singapore
    Posts
    7
    Rep Power
    0

    Default

    can u give me more details in code

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Can you find the code, where user informations gets in to the application. Look at the main method. Did you find it?

  5. #5
    karthik7974 is offline Member
    Join Date
    Apr 2008
    Location
    singapore
    Posts
    7
    Rep Power
    0

    Default

    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

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  7. #7
    karthik7974 is offline Member
    Join Date
    Apr 2008
    Location
    singapore
    Posts
    7
    Rep Power
    0

    Default

    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.

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    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.

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

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Here what you want, do it with busy works. Test and let me know.

    Java 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:|");
    	}

  10. #10
    karthik7974 is offline Member
    Join Date
    Apr 2008
    Location
    singapore
    Posts
    7
    Rep Power
    0

    Default

    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

    .:)

  11. #11
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    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?

  12. #12
    DonCash's Avatar
    DonCash is offline Moderator
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    240
    Rep Power
    9

    Default

    You need something like this:

    Java 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.
    Last edited by DonCash; 04-21-2008 at 05:23 PM. Reason: Making a few corrections...
    Did this post help you? Please me! :cool:

  13. #13
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    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!

  14. #14
    DonCash's Avatar
    DonCash is offline Moderator
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    240
    Rep Power
    9

    Default

    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 me! :cool:

  15. #15
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    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. :)

  16. #16
    DonCash's Avatar
    DonCash is offline Moderator
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    240
    Rep Power
    9

    Default

    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 me! :cool:

  17. #17
    karthik7974 is offline Member
    Join Date
    Apr 2008
    Location
    singapore
    Posts
    7
    Rep Power
    0

    Thumbs up

    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.

  18. #18
    DonCash's Avatar
    DonCash is offline Moderator
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    240
    Rep Power
    9

    Default

    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.
    Last edited by DonCash; 04-21-2008 at 05:38 PM.
    Did this post help you? Please me! :cool:

  19. #19
    DonCash's Avatar
    DonCash is offline Moderator
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    240
    Rep Power
    9

    Default

    Try this code. This will read in the input file (input.txt) and output whatever you set as FindMe to output.txt. Note: Make sure input.txt is in the same location as the code.

    Example input file:

    Java Code:
    10/10/2008,server1,xxx.xxx.xxx.xxx
    10/10/2008,server2,xxx.xxx.xxx.xxx
    12/04/2008,server1,xxx.xxx.xxx.xxx
    16/07/2008,server1,xxx.xxx.xxx.xxx
    Code:

    Java 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 Inputpath;
    	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(Inputpath);
    			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();
    		
             Inputpath = "input.txt";
    		 FindMe = "10/10/2008";
    		
    		sf.openFile();
    				
    	}
    }
    This code is looking for: 10/10/2008

    So the output will be:

    Java Code:
    10/10/2008,server1,xxx.xxx.xxx.xxx
    10/10/2008,server2,xxx.xxx.xxx.xxx
    Last edited by DonCash; 04-21-2008 at 05:53 PM.
    Did this post help you? Please me! :cool:

  20. #20
    karthik7974 is offline Member
    Join Date
    Apr 2008
    Location
    singapore
    Posts
    7
    Rep Power
    0

    Thumbs up

    Hi Don,

    I received the below error

    StringFind.java:26: cannot resolve symbol
    symbol : method contains (java.lang.String)
    location: class java.lang.String
    if (strLine.contains(FindMe)) {
    ^
    1 error


    I tried to solve cannot, pls advice

Page 1 of 2 12 LastLast

Similar Threads

  1. JSP Parser????
    By chathu03j in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 04-10-2008, 01:08 PM
  2. DNS name parser 1.2.1
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 07-14-2007, 09:21 PM
  3. XML Parser
    By samfuerte in forum XML
    Replies: 1
    Last Post: 07-14-2007, 05:14 PM
  4. Parser
    By Peter in forum Advanced Java
    Replies: 2
    Last Post: 07-04-2007, 08:08 AM
  5. Dom Parser
    By Marcus in forum Advanced Java
    Replies: 2
    Last Post: 07-02-2007, 07:47 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •