-
Notifications
You must be signed in to change notification settings - Fork 475
/
Copy pathtrim.rb
55 lines (40 loc) · 878 Bytes
/
trim.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
#!/usr/bin/env ruby
module State
LINE_NUMBER = 1
TIME = 2
SUB = 3
SPACE = 4
end
input_file = ARGV[0]
output_file = ARGV[1] ? ARGV[1] : ARGV[0]
if input_file && output_file
trimmed = []
File.read(input_file).each_line do |line|
trimmed << line.strip
end
result = ""
last_state = nil
trimmed.each do |line|
case line
when /^\d+$/
result += "\n" + line
last_state = State::LINE_NUMBER
when /^\d+:\d+:\d+,\d+\s-->\s\d+:\d+:\d+,\d+$/
result += "\n" + line
last_state = State::TIME
when /^$/
result += "\n" + line
last_state = State::SPACE
else
if last_state != State::SUB
result += "\n" + line
else
result += " " + line
end
last_state = State::SUB
end
end
File.open(output_file, 'w') do |file|
file.write(result)
end
end