Comparing text in two files

sdaccord01

Senior member
Jul 9, 2003
291
0
0
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.
 

n0cmonkey

Elite Member
Jun 10, 2001
42,936
1
0
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):
 

n0cmonkey

Elite Member
Jun 10, 2001
42,936
1
0
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.
 

sdaccord01

Senior member
Jul 9, 2003
291
0
0
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.
 

sdaccord01

Senior member
Jul 9, 2003
291
0
0
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.
 

n0cmonkey

Elite Member
Jun 10, 2001
42,936
1
0
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.
 

n0cmonkey

Elite Member
Jun 10, 2001
42,936
1
0
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...
 

sdaccord01

Senior member
Jul 9, 2003
291
0
0
n0c, I figured it out, used chomp for the first statements of the while loops and everything works,thanks!
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |