I need several lines combined to one line with the matching serialnumber
in front.
I’m not good at coming up with the code examples off the top of my head
but maybe a regex matching the serial numbers and then a split to pull
off the portion you want then combining the matched portion.
On Apr 27, 2009, at 2:59 PM, Jesús Gabriel y Galán wrote:
00-04-00;Elektrischer Anschluss: 230V / 50Hz
s = “00-40…” #your string
Jesus.
current_serial = nil
texts = []
File.open(outputfilename, ‘w’) do |out|
File.foreach(filename) do |line|
serial, text = line.chomp.split(‘;’, 2)
if current_serial && serial != current_serial
out.puts “#{current_serial};#{texts.join(’ ')}”
texts = []
end
current_serial = serial
texts << text
end
if current_serial
out.puts “#{current_serial};#{texts.join(’ ')}”
end
end
Two things of note: The second argument to split(‘;’, 2) limits the
result to 2 items so if there happens to be a ‘;’ later in the line it
isn’t considered a place to split. Keeping the items in a hash
doesn’t guarantee the order on the way back out, but I’m also assuming
that all the lines with a given serial number are together.
You have to initialize your filename and outputfilename, of course.
-Rob