Parsing Fixed Width File

Hello, all.
Does anyone know of a good or any for that matter library (gem) to
parse fixed width files?

I’ve looked into active-warehouse - but it looks like it’s for rails
only and docs are a bit underdeveloped for it.

Any ideas anyone?

Did you look at the ActiveWarehouse ETL docs:
http://activewarehouse.rubyforge.org/docs/activewarehouse-etl.html ?
If nothing else you might be able to look at the existing fixed width
file parser and use that as a starting point.

Sincerely,
Anthony E.

On Tue, Apr 14, 2009 at 6:13 PM, Nick da G [email protected]
wrote:


GMU/IT d- s: a32 C++(++++)$ UL@ P— L+(++) !E W+++$ !N o? K? w— !O
M++ V PS+ PE Y PGP t+ !5 X- R tv b++ DI+ D++ G- e++ h---- r+++ y++++**

http://anthony.mp

On Wed, Apr 15, 2009 at 12:13 AM, Nick da G [email protected]
wrote:

Hello, all.
Does anyone know of a good or any for that matter library (gem) to
parse fixed width files?

I’ve looked into active-warehouse - but it looks like it’s for rails
only and docs are a bit underdeveloped for it.

Any ideas anyone?

It’s pretty simple so I’m not sure that a library is needed

fields = [5,12,5,2,20] #<- the size of each field
field_pattern = “A#{fields.join(‘A’)}”
File.foreach(filename) do |line|
row = line.unpack(field_pattern)
#use row[0] etc for each field
end

Andrew T.
http://ramblingsonrails.com
http://www.linkedin.com/in/andrewtimberlake

“I have never let my schooling interfere with my education” - Mark Twain

Anthony E. wrote:

Did you look at the ActiveWarehouse ETL docs:
http://activewarehouse.rubyforge.org/docs/activewarehouse-etl.html ?
If nothing else you might be able to look at the existing fixed width
file parser and use that as a starting point.

Sincerely,
Anthony E.

Yeah I looked into it - still requires use of ActiveRecord.

It’s pretty simple so I’m not sure that a library is needed

fields = [5,12,5,2,20] #<- the size of each field
field_pattern = “A#{fields.join(‘A’)}”
File.foreach(filename) do |line|
row = line.unpack(field_pattern)
#use row[0] etc for each field
end

Andrew T.
http://ramblingsonrails.com
http://www.linkedin.com/in/andrewtimberlake

“I have never let my schooling interfere with my education” - Mark Twain

Yeah that’s pretty much what I ended up doing.
Created an object that defines each record (line)

then pass it at a line from file, and get an object back that I can do
what I want with - like outputing to csv .

I was jsut wondering if there was a more simple and elegant solution.
But I guess you can’t go simpler than that. :slight_smile: Thanks for a response!