Parsing files

B

barjunk

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
 
L

Logan Capaldo

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.
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 ;).
 
R

Robert Klemme

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,992
Messages
2,570,220
Members
46,807
Latest member
ryef

Latest Threads

Top