Skip to content

Instantly share code, notes, and snippets.

@zerebral
Created August 29, 2017 14:01
Show Gist options
  • Save zerebral/dfc268c55cd26a6c7077301ced0670e3 to your computer and use it in GitHub Desktop.
Save zerebral/dfc268c55cd26a6c7077301ced0670e3 to your computer and use it in GitHub Desktop.
QnD script to scan VINs in all files in a directory recursively
letter_value = {'A' => 1, 'B' => 2, 'C' => 3, 'D' => 4,
'E' => 5, 'F' => 6, 'G' => 7, 'H' => 8,
'J' => 1, 'K' => 2, 'L' => 3, 'M' => 4,
'N' => 5, 'P' => 7, 'R' => 9, 'S' => 2,
'T' => 3, 'U' => 4, 'V' => 5, 'W' => 6,
'X' => 7, 'Y' => 8, 'Z' => 9, '1' => 1,
'2' => 2, '3' => 3, '4' => 4, '5' => 5,
'6' => 6, '7' => 7, '8' => 8, '9' => 9, '0' => 0,
'a' => 1, 'b' => 2, 'c' => 3, 'd' => 4,
'e' => 5, 'f' => 6, 'g' => 7, 'h' => 8,
'j' => 1, 'k' => 2, 'l' => 3, 'm' => 4,
'n' => 5, 'p' => 7, 'r' => 9, 's' => 2,
't' => 3, 'u' => 4, 'v' => 5, 'w' => 6,
'x' => 7, 'y' => 8, 'z' => 9}
position_weight = [8, 7, 6, 5, 4, 3, 2, 10, 0, 9, 8, 7, 6, 5, 4, 3, 2]
def get_check_digit(vin)
total = 0
for i in 0...17
total += letter_value[vin[i]] * position_weight[i] rescue 0
end
return ((total % 11) == 10) ? "X" : "#{(total % 11)}"
end
array_of_all_files = Dir.glob("#{ARGV[0]}/*").reject { |file_path| File.directory? file_path }
array_of_all_files.each {|f|
puts "Reading file - #{f}"
content = File.read(f)
scanned_vins = content.scan(
/
[A-H,J-N,P-Z,1-9]{1} #should not begin with zero '0'
[A-H,J-N,P-Z,0-9]{8}
[A-H,J-N,P-T,V-Y,1-9]{1} #year should not contain U, Z or zero '0'
[A-H,J-N,P-Z,0-9]{4}
[0-9]{3}
/ix).flatten rescue []
puts "Vins? #{scanned_vins.select{|x| x[8] == get_check_digit(x)}.inspect}"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment