Created
October 24, 2012 08:24
-
-
Save rklemme/3944819 to your computer and use it in GitHub Desktop.
Diff two sorted files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/ruby | |
def trace(old, new) | |
printf "%p old=%p new=%p\n", caller[0], old, new | |
end | |
def fetch(io) | |
s = io.gets and s.chomp! | |
s | |
end | |
def cmp(old, new) | |
if old.nil? || (new && old > new) | |
return '+', new | |
elsif new.nil? || (old && old < new) | |
return '-', old | |
else | |
return ' ', old | |
end | |
end | |
old_file, new_file = ARGV.shift 2 | |
File.open old_file do |of| | |
File.open new_file do |nf| | |
r_old = fetch(of) | |
r_new = fetch(nf) | |
begin | |
#trace r_old, r_new | |
flag, s = cmp(r_old, r_new) | |
printf "%s%s\n", flag, s | |
case flag | |
when '-' | |
r_old = fetch(of) | |
when '+' | |
r_new = fetch(nf) | |
when ' ' | |
r_old = fetch(of) | |
r_new = fetch(nf) | |
else | |
raise 'programmer error' | |
end | |
end while r_old || r_new | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment