Skip to content

Instantly share code, notes, and snippets.

@oogali
Created October 14, 2011 19:11
Show Gist options
  • Save oogali/1288028 to your computer and use it in GitHub Desktop.
Save oogali/1288028 to your computer and use it in GitHub Desktop.
Unified diff of strings in Ruby (Two nmap scans)
require 'rubygems'
require 'diff/lcs'
require 'diff/lcs/string'
require 'diff/lcs/hunk'
scan = [
%x[nmap -nsP -oG - 192.168.1.0/24].split(/\n/).map! { |e| e.chomp },
%x[nmap -nsP -oG - 192.168.1.0/24].split(/\n/).map! { |e| e.chomp }
]
output = ''
oldhunk = hunk = nil
len = 0
diffs = Diff::LCS.diff scan[0], scan[1]
diffs.each do |diff|
begin
hunk = Diff::LCS::Hunk.new(scan[0], scan[1], diff, 1, len)
len = hunk.file_length_difference
next unless oldhunk
if hunk.overlaps?(oldhunk) then
hunk.unshift(oldhunk)
else
output << oldhunk.diff(:unified)
end
ensure
oldhunk = hunk
output << "\n"
end
end
output << oldhunk.diff(:unified)
output << "\n"
puts output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment