2015-02-20 5 views
0

Я хочу проверить два файла 'file1.txt' и 'file2.txt' и распечатать линии, которые являются общими. Как мне это сделать? Ниже то, что у меня есть в файлах:Как сравнить два файла по строкам

file1.txt:

An insightful look into the scenario of academic integrity and its implications give us the major motivation 
for pursuing the subject. The issue holds utmost significance as the intellectual standards of an individual 
pursuing an academia a reestablished around his ability to produce authoritative work. Plagiarism is thus lethal. 
Every year a large number of students and scholars submit a huge volume of material to their respective mentors and professors 

file2.txt:

An insightful look into the scenario of academic integrity and its implications give us the major motivation 
for pursuing the subject. The issue holds utmost significance as the intellectual standards of an individual 
pursuing an academia a reestablished around his ability to produce authoritative work. Plagiarism is thus lethal. 
Every year a large number of students and scholars submit a huge volume of material to their respective mentors and professors. 
Due to the sheer amount of text involved, a manual Result and conclusion follow where we present our observations and learning. 
+0

ваш код не работает? –

+0

код, который я опубликовал, работает! но в том, что он просто сравнивает весь файл. Я хочу проверить строку за строкой и распечатать только те строки, которые равны –

+0

Становится все более похожим на XY-проблему. – sawa

ответ

2

Вы также можете использовать внешние итераторы для сравнения пары строк из разных файлов один на один, что-то вроде этого:

lines1 = File.readlines('file1.txt').each 
lines2 = File.readlines('file2.txt').each 

begin 
    i = 0 
    while true 
    puts "line #{i +=1 }:" 
    puts line1 = lines1.next 
    puts line2 = lines2.next 
    puts "identical: #{line1 == line2 ? 'yes' : 'no'}\n\n" 
    end 
rescue StopIteration 
end 

Если вы не заботитесь о начальных и конечных пробелах, то вы можете используйте String#strip - line1.strip == line2.strip. Цикл остановится, когда и конец любого файла будет достигнут.

Выход производится с line1.strip == line2.strip выглядит следующим образом:

line 1: 
An insightful look into the scenario of academic integrity and its implications give us the major motivation 
An insightful look into the scenario of academic integrity and its implications give us the major motivation 
identical: yes 

line 2: 
for pursuing the subject. The issue holds utmost significance as the intellectual standards of an individual 
for pursuing the subject. The issue holds utmost significance as the intellectual standards of an individual 
identical: yes 

line 3: 
pursuing an academia a reestablished around his ability to produce authoritative work. Plagiarism is thus lethal. 
pursuing an academia a reestablished around his ability to produce authoritative work. Plagiarism is thus lethal. 
identical: yes 

line 4: 
Every year a large number of students and scholars submit a huge volume of material to their respective mentors and professors 
Every year a large number of students and scholars submit a huge volume of material to their respective mentors and professors. 
identical: no 

line 5: 
2

Edited следующее предложение Arup в.

puts File.readlines("file1.txt") & File.readlines("file2.txt") 
+0

Хе-хе. Это так просто, кажется ... :) –

+1

do 'File.readlines (имя_файла)' ........ –

+0

Я знаю .. Ваш ответ правильный. –

Смежные вопросы