-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcleanup_t3.rb
executable file
·60 lines (47 loc) · 1.17 KB
/
cleanup_t3.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
#!/usr/bin/env ruby
require 'find'
require 'fileutils'
## load the base class
require File.join(File.dirname($0), 'base.rb')
class Worker < Base
def fix(input, output)
data = []
File.open(input, 'r').each do |line|
clean_line = line.chars.select(&:valid_encoding?).join
.gsub(/\r\n?/,'|')
.clean_up
if clean_line.include?('|')
clean_line.split('|').each do |s|
data << s.strip
end
else
data << clean_line
end
end
info "saving #{input} to #{output}"
File.open(output, 'w+') do |f|
f.puts(data)
end
end
end
### functions
def usage
puts 'Usage: ' + File.basename(__FILE__) + ' <directory>'
exit 1
end
usage unless ARGV.length == 1 and File.directory?(ARGV[0])
worker = Worker.new
source_dir = ARGV[0]
output_dir = 'fixed'
# create the output directory
FileUtils.mkdir(output_dir) unless File.directory?(output_dir)
Find.find(source_dir) do |input|
if File.directory?(input)
next
else
if File.extname(input) == '.txt' and not File.basename(input) =~ /^\./
output = File.join(output_dir, File.basename(input))
worker.fix(input, output)
end
end
end