Comparing two log files ignoring the lines
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
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.)
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
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.
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.
Maybe this can be a starting point...