-
Notifications
You must be signed in to change notification settings - Fork 6
/
rubycsv.rb
executable file
·104 lines (83 loc) · 2.49 KB
/
rubycsv.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/usr/bin/env ruby
require 'csv'
require 'erb'
require 'date'
# globals
$template_file = ARGV[0]
$csv_filename = ARGV[1]
$template_text = ""
$header_row = []
# evaluate the template in order to get the templates and other variables out
eval(File.read($template_file))
$template_text = $erb_template
# class to bind hash into erb template
class CSVRow
attr_accessor :csvrow
def initialize(row)
@csvrow = Hash.new
index = 0
$header_row.size.times do
@csvrow[$header_row[index]] = row[index]
index +=1;
end
end
def get_binding()
return binding()
end
end
# helper functions
def clean_date(text_date, date_format="%Y-%m-%d") # reformat date in to big endian format
return Date.strptime(text_date,date_format).strftime("%Y-%m-%d")
end
def clean_money(text_money, positive = FALSE) # nuke all but digits, negation, decimal place
if text_money.nil?
return ""
end
if positive
return text_money.gsub(/[^\d\.]/,'')
else
return text_money.gsub(/[^-\d\.]/,'')
end
end
def clean_num(text_num) # nuke all but digits, negation
if text_num.nil?
return ""
end
return text_num.gsub(/[^-\d]/,'')
end
def clean_text(text) # get rid of anything that's not a word or space, make multiple spaces single space
if text.nil?
return ""
end
return text.gsub(/[^\w \]\[\@\.\,\'\"]/,'').gsub(/[ ]+/,' ').strip;
end
# lookup values in arrays. Table is a 2D array,
# where the first value is the string returned,
# and the following are substrings to attempt to match against the value
def tablematch(table, value)
default = ""
table.each do |row|
rowfirst = row.first
row.slice(1,row.length).each do |item|
if item == "DEFAULT" # set the default value
default = rowfirst
elsif value.match(/^#{item}/i)
#print " val: #{value} matches #{item}, returning #{rowfirst}\n"
return rowfirst
end
end
end
return default
end
# parse CSV file
csv = CSV.open($csv_filename,'r')
csv.each() do |row|
if(csv.lineno == 1) #this is the header row, store to assign as keys on later rows, stripping whitespace
$header_row = row.collect{|val| val.to_s.strip}
else
thisrow = CSVRow.new(row)
#print "line: #{thisrow.csvrow.inspect}\n"
erbrender = ERB.new($erb_template, nil, '<>')
puts erbrender.result(thisrow.get_binding)
end
end