Forum: Ruby Parsing Fixed Width File

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Fd1a879445f337144bc1f2a5ebd8402b?d=identicon&s=25 nick ger (konung)
on 2009-04-15 00:13
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?
31254903db793bf6f84bbd607fe092fd?d=identicon&s=25 Anthony Eden (Guest)
on 2009-04-15 01:16
(Received via mailing list)
Did you look at the ActiveWarehouse ETL docs:
http://activewarehouse.rubyforge.org/docs/activewa... ?
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 Eden

On Tue, Apr 14, 2009 at 6:13 PM, Nick da G <nick.gorbikoff@gmail.com>
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
5772c599ccab3081e0fffb1d54f3b6de?d=identicon&s=25 Andrew Timberlake (andrewtimberlake)
on 2009-04-15 05:09
(Received via mailing list)
On Wed, Apr 15, 2009 at 12:13 AM, Nick da G <nick.gorbikoff@gmail.com>
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 Timberlake
http://ramblingsonrails.com
http://www.linkedin.com/in/andrewtimberlake

"I have never let my schooling interfere with my education" - Mark Twain
Fd1a879445f337144bc1f2a5ebd8402b?d=identicon&s=25 nick ger (konung)
on 2009-04-15 19:31
Anthony Eden wrote:
> Did you look at the ActiveWarehouse ETL docs:
> http://activewarehouse.rubyforge.org/docs/activewa... ?
> 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 Eden
>


Yeah I looked into it - still requires use of ActiveRecord.
Fd1a879445f337144bc1f2a5ebd8402b?d=identicon&s=25 nick ger (konung)
on 2009-04-15 19:34
> 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 Timberlake
> 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. :-) Thanks for a response!
This topic is locked and can not be replied to.