Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Comparing two log files ignoring the lines

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Posts
    4
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Comparing two log files ignoring the lines

    Hello Friends,
    am trying to compare two log files using java, but in that comparision i would like to ignore some lines which strats with Rate, start, stop....etc.

    and i would like to compare them avoiding the first 14 characters (DATE and time format)of the each line.

    Dont know how to achieve this please suggest me in this
    Thank you in advance


  2. #2
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Comparing two log files ignoring the lines

    If the files have the same number of lines and they are "in sync," then it's not too tough. If one has some "extra" lines with nothing corresponding in the other file, then line-by-line comparisons will be ineffective after the first such discrepancy is found.

    Maybe there are no "extra" lines in one file or the other you can start with something simple. Even if there can be "extra" lines, the problem of keeping the files "in sync" can be addressed after the basic functionality is implemented.

    So, consider the following:

    Prepare a boolean function ignoreLine(String str) that will test strings beginning with certain substrings. It will return "true" if the string begins with "Rate", "start", "stop", or whatever sweetness makes you want to ignore a line that begins with that string. If it does not begin with any of your specific key words or phrases, return "false."

    Then the main() function could go something like this:
       Set up Scanner objects for each file.
     
       Here's the loop to process the files (I haven't shown the
       things you do to make sure all lines of both files are read
       and what you think you should do for various discrepancies.)
     
       BEGIN LOOP
     
           Read from file 1 into String str1
           Read from file 2 into String str2
     
           If you want to ignore the first 14 characters, then
           create two new strings str1a and str2a:
     
           String str1a = str1.substring(15); String str2a = str2.substring(15);
     
            Preserve the original strings at least for debugging purposes.
            After debugging, you still might want to have the original
            lines available for whatever kind of report you are generating.
     
           (Note that if the lines have fewer than 14 characters they will require
           special treatment.  Maybe you don't think this is necessary, but I think
           a program should always be prepared to deal with adversity such as
           unexpected input.)
     
            Anyhow...
     
     
           Call your ignoreLine() function separately for str1a and str2a.
     
           If the results are not the same, then you have a big problem.
           Don't know whether the files are out of sync or what...
           A "real" program must have a strategy for this, but maybe
           you can be thinking about how you will handle this while you
           continue with the first-pass implementation.
     
           Anyhow...
     
           If the ignoreLine() function for both returns "true" then go
           on to the next line.
     
           If the ignoreLine() function for both returns "false" then
           use the String isEqual() method to compare the strings and report
           results according to your requirements.
     
       END LOOP


    Maybe this can be a starting point...


    Cheers!


    Z

Similar Threads

  1. Ignoring cases
    By noel222 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: February 11th, 2012, 06:34 PM
  2. Reading lines in files with tabs
    By imsuperman05 in forum Java Theory & Questions
    Replies: 1
    Last Post: January 14th, 2012, 12:05 PM
  3. Comparing two files and printing out matching words
    By sport10 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 3rd, 2010, 09:10 PM
  4. Are We seriously Ignoring NetBeans?
    By javacrazed in forum Java IDEs
    Replies: 4
    Last Post: November 12th, 2008, 05:08 PM