• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Comparing text in two files

sdaccord01

Senior member
I need help in writing a program in either Java or Perl on file input and comparing two files and their lines of text.

For example, if file1 has:

I like oranges
I like apples
I like pears

and file2 has:

I like apples
I like guavas
I like oranges

then running the program with this command:

samelines file1 file2

Should produce the output:

I like apples
I like oranges

Can somebody help? Thanks in advance.
 
This is quick, dirty, and doesn't fit all of the requirements (I don't feel like looking up how to specify files at the moment):
 
Originally posted by: sdaccord01
Thanks for your help, I appreciate the example.

It gave me a chance to play with perl a bit more, I need to come up with more projects for myself or I'll forget the TINY bit I know and never get around to learning more. 🙁
 
Hey n0c, I tried to run your code with the two files I created, but I think with you're doing line to line comparison, rather than taking into account if the one line is on line 1 of the first file and the matching line is on line 2 of the second file. I just started to learn perl, and I understand how to approach this programming problem just not how to implement it. It seems like you would have to take line 1 of the first file and compare it to all of the lines on the second file and if it compares to any one of them, then do the same for line 2 of the first file, etc.
 
Oh wait, it does, sorry. For some reason, when i have those three elements when:

file1 has:

I like oranges
I like apples
I like pears

and file2 has:

I like apples
I like guavas
I like oranges

It only returns "I like apples"

But when I put in the same fourth line on both "I like bananas" it includes the oranges that it was supposed to have in the first place, kind of weird.
 
Originally posted by: sdaccord01
Hey n0c, I tried to run your code with the two files I created, but I think with you're doing line to line comparison, rather than taking into account if the one line is on line 1 of the first file and the matching line is on line 2 of the second file. I just started to learn perl, and I understand how to approach this programming problem just not how to implement it. It seems like you would have to take line 1 of the first file and compare it to all of the lines on the second file and if it compares to any one of them, then do the same for line 2 of the first file, etc.

Im attaching the same code (with one fix 😱) with some comments that should help you out. All of the lines in file1 should be compared to all of the lines in file2. It worked during my tests anyhow. 😉

Here are the files I compared:

more file_compare?
::::::::::::::
file_compare1
::::::::::::::
This is line 1.
This is line 5.
This is line three.
This is line 4.
This is line 2.
::::::::::::::
file_compare2
::::::::::::::
This is line one.
This is line 2.
This is line three.
This is line four.

Here is the output after I run the program:

more file_out
These are the lines that are the same:
This is line three.
This is line 2.
 
Originally posted by: sdaccord01
Oh wait, it does, sorry. For some reason, when i have those three elements when:

file1 has:

I like oranges
I like apples
I like pears

and file2 has:

I like apples
I like guavas
I like oranges

It only returns "I like apples"

But when I put in the same fourth line on both "I like bananas" it includes the oranges that it was supposed to have in the first place, kind of weird.

It worked fine for me. The script probably doesn't like extra white space at the end of lines, so check for extra spaces. 😉

It can probably be modified to not care, but that's beyond me ATM. Maybe chop or chomp or something related...
 
Back
Top