Skip to content

Instantly share code, notes, and snippets.

@rymawby
Created January 20, 2012 15:34
Show Gist options
  • Save rymawby/1647904 to your computer and use it in GitHub Desktop.
Save rymawby/1647904 to your computer and use it in GitHub Desktop.
Ruby script to compare two text files - analysed what lines exist in file1 that do not in file2.
#!/usr/bin/ruby
# script to compare and see what lines are in file1 but not file2
f1 = File.open('file1.txt')
f2 = File.open('file2.txt')
file1lines = f1.readlines
file2lines = f2.readlines
file1lines.each do |e|
if(!file2lines.include?(e))
 puts e
end
end
@connelevalsam
Copy link

Hello...
How about if you want to compare two files if everything in file1.txt matches that in file2.txt?
And if file2.txt is shorter, it should copy the remaining lines from file1.txt into it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment