-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparse_file.rb
65 lines (53 loc) · 1.32 KB
/
parse_file.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# encoding: utf-8
require 'csv'
require_relative 'parse_line'
require_relative 'get_column_widths'
filename = ARGV[0]
if filename == nil then
puts "Usage: ruby parse_file.rb filename.txt."
Process.exit(1)
end
if not File.file?(filename) then
puts "Filename '" + filename + "' doesn't seem to exist or is not a file."
Process.exit(2)
end
column_names_line = nil
column_names = nil
column_sizes = nil
printed_header = false
error_count = 0
CSV do |csv|
File.open(filename, "r", :encoding => 'windows-1250').each do |line|
next if /^Records affected/ =~ line # Last line in database export.
if /^\s*$/ =~ line then
# Blank line.
column_sizes = nil
column_names_line = nil
next
end
if /^[=\s]+$/ =~ line then # Underscore line.
next if column_sizes != nil
column_sizes = get_column_widths line
column_names = extract_values column_names_line, column_sizes
if not printed_header then
csv << column_names
printed_header = true
end
next
end
next if line == column_names_line
if column_sizes != nil then
begin
csv_values = extract_values line.encode("utf-8"), column_sizes
csv << csv_values
rescue
error_count += 1
$stderr.puts line
# Process.exit(3)
end
else
column_names_line = line
end
end
end
$stderr.puts "Number of encoding errors: #{error_count}."