From: Robert Klemme Date: 2007-01-12T23:15:10+09:00 Subject: Re: Splitting a multirecord per file format to a single record per file format: Right approach? On 12.01.2007 15:02, Randy Kramer wrote: > I'm trying to write essentially what I guess you'd call a filter (or maybe not > quite exactly). It needs to: > > * read multi-line records from a file (one record at a time) > * then, with that one record: > * prepend some additional lines > * make substitutions for some of the lines already in the record > * grab some other portions of the record (less than a line, but usually > multiple words), find the "non-null" pieces, and incorporate those in > another header line > * create a unique filename > * write that (single) record to that file [...] > 3. The content of the files I have to convert is actually more like this: > > > Record header ('\x80\x81\x82\x83') > > Record (with blank lines) > (trailing blank line) > Record header ('\x80\x81\x82\x83') > > Record (with blank lines) > (trailing blank line) > Record header ('\x80\x81\x82\x83') > > Record (with blank lines) > You could do: # create a class for your records or use OpenStruct YourRecord = Struct.new :name, :length, :foo, :bar def dump() File.open(file_name, "w") do |io| # whatever end end end current = nil File.foreach('your file') do |line| line.chomp! case line when /^$/ current = YourRecord.new when /^$/ current.dump current = nil when /Record header/ ... else # ignore or whatever end end Kind regards robert