forked from matpalm/resemblance
-
Notifications
You must be signed in to change notification settings - Fork 0
/
split.rb
executable file
·39 lines (31 loc) · 890 Bytes
/
split.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
#!/usr/bin/env ruby
single_export = ARGV.include? 'single_export'
`mkdir split 2>/dev/null`
if single_export
nap = File.open('split/nap','w')
else
names, addresses, phones = ['names','addresses','phones'].collect { |f| File.open("split/#{f}",'w') }
end
STDIN.each do |line|
cols = line.chomp.split '|'
id = cols[0]
name = cols[1]
address_join_char = single_export ? '|' : ' '
address = [2,3].collect{|i| cols[i]}.join(address_join_char).strip
if single_export
phone = cols[6]
name_addr_phone = [name,address,phone].join('|')
nap.puts "#{id}|#{name_addr_phone}"
else
phone = cols[4]
raise "empty name? [#{line}]" if name.empty?
names.puts "#{id}|#{name}"
addresses.puts "#{id}|#{address}" unless address.empty?
phones.puts "#{id}|#{phone}" unless phone.empty?
end
end
if single_export
nap.close
else
[names, addresses, phones].each { |f| f.close }
end