Pavel
Pavel

Reputation: 63

Split file into two tempfiles in ruby

I have a large file with 2 different formats separated by a dashed line, how can I split the file into two tempfiles for processing?

Example:

yaml:format
yaml:format
yaml:format
---------
csv,format
csv,format
etc.

Upvotes: 1

Views: 116

Answers (1)

Patrick Oscity
Patrick Oscity

Reputation: 54734

split at exactly twelve dashes:

yaml, csv = input.split('------------', 2)

or at a variable number of dashes

yaml, csv = input.split(/^-+$/, 2)

this will produce empty lines around the delimiter (end of yaml and start of csv), if you want to get rid of them you can do

yaml, csv = input.split(/[\r\n]+^-+$[\r\n]+/, 2)

Upvotes: 1

Related Questions