# Parsing files

**URL:** <https://www.ruby-forum.com/t/parsing-files/71215>\
**Category:** Ruby\
**Created:** [October 6, 2006, 3:16am UTC](https://www.ruby-forum.com/t/parsing-files/71215 "2006-10-06T03:16:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![barjunk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ec8ea/32.png) [@barjunk](https://www.ruby-forum.com/u/barjunk)\
**Post date:** [October 6, 2006, 3:16am UTC](https://www.ruby-forum.com/t/parsing-files/71215/1 "2006-10-06T03:16:12Z")

</div>

I’m new to ruby and one of the first projects I am trying to tackle is  
parsing a file  
with this sort of format:

zone “localdomain” IN {  
type master;  
file “localdomain.zone”;  
allow-update { none; };  
};

or host devicename-1 {  
hardware ethernet 00:10:51:61:74:FF;  
fixed-address 192.168.0.15;  
}

The basic issue I’m working to resolve is how can I treat these  
sections as “records”.

I know I could read the entire file into memory, but I was looking to  
only deal with them  
on a record by record basis.

One thought was to read through the file and keep a hash of information  
about where the  
record starts and ends, then when I want to read a record, use that  
reference to get the  
data.

Has anyone done something similar to this that I can use to learn how  
to manage these  
types of file formats?

There won’t be an issues with concurrent access at this point.

These formats are found when using dhcp, freeradius, and nagios. I’m  
sure there are others.

Mike

---

<div class="post-metadata">

**Author:** ![barjunk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ec8ea/32.png) [@barjunk](https://www.ruby-forum.com/u/barjunk)\
**Post date:** [October 6, 2006, 3:30am UTC](https://www.ruby-forum.com/t/parsing-files/71215/2 "2006-10-06T03:30:13Z")

</div>

On Fri, Oct 06, 2006 at 10:15:12AM +0900, barjunk wrote:

> or host devicename-1 {  
> on a record by record basis.
> 
> There won’t be an issues with concurrent access at this point.
> 
> These formats are found when using dhcp, freeradius, and nagios. I’m  
> sure there are others.

I would write a grammar and a recursive descent parser for files like  
these. Someone is bound to reply with some crazy regexp that works, but  
only they will ever understand it ;).

---

<div class="post-metadata">

**Author:** ![barjunk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ec8ea/32.png) [@barjunk](https://www.ruby-forum.com/u/barjunk)\
**Post date:** [October 6, 2006, 9:40am UTC](https://www.ruby-forum.com/t/parsing-files/71215/3 "2006-10-06T09:40:48Z")

</div>

On 06.10.2006 03:28, Logan C. wrote:

> I would write a grammar and a recursive descent parser for files like  
> these. Someone is bound to reply with some crazy regexp that works, but  
> only they will ever understand it ;).

:-)) Basically it depends on how much nesting of brackets is allowed.  
If there is indefinite nesting, then yes, a proper parser is the best  
solution. If it is just two levels as shown in the example a regexp is  
not too hard (to write and understand) IMHO.

Kind regards

```
robert
```
