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.

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