Hello!
I need to parse a specific file to a hash. This is a sample of the file:
blablabla......
}
-- 1.3.6.1.4.1.000.6.1.2.2.11.1
mASLocked OBJECT-TYPE
SYNTAX INTEGER
......blablabla
And my hash has to be like this:
my_hash = {
'mASLocked' => '1.3.6.1.4.1.000.6.1.2.2.11.1'
}
The hash will have about 400 items on it.
The only 2 things that are standard on the file are the '-- ' before the
number and the string 'OBJECT-TYPE' on the second line.
I saw a lot of parse examples but they always use something like DATA
SEPARATION DATA. This is not my case since the identifier is in one line
before the data and in the other line after the data I want.
I am thinking of doing something very ugly here. Any ideas to save me
from do a ugly Frankenstein (and probably wrong) code?
Thanks
···
--
Posted via http://www.ruby-forum.com/.
You might try String#scan with a multiline regex.
text =<<EOF
-- 1.3.6.1.4.1.000.6.1.2.2.11.1
mASLocked OBJECT-TYPE
SYNTAX INTEGER
EOF
hash = Hash.new
regex = /-- (.+)\n\s+(.+) OBJECT-TYPE/m
text.scan(regex) do |value,key|
hash[key] = value
end
p hash
p hash
···
On Thu, May 6, 2010 at 3:37 PM, Joao Pompei <joao.pompei@gmail.com> wrote:
Hello!
I need to parse a specific file to a hash. This is a sample of the file:
blablabla......
}
-- 1.3.6.1.4.1.000.6.1.2.2.11.1
mASLocked OBJECT-TYPE
SYNTAX INTEGER
......blablabla
And my hash has to be like this:
my_hash = {
'mASLocked' => '1.3.6.1.4.1.000.6.1.2.2.11.1'
}
The hash will have about 400 items on it.
The only 2 things that are standard on the file are the '-- ' before the
number and the string 'OBJECT-TYPE' on the second line.
I saw a lot of parse examples but they always use something like DATA
SEPARATION DATA. This is not my case since the identifier is in one line
before the data and in the other line after the data I want.
I am thinking of doing something very ugly here. Any ideas to save me
from do a ugly Frankenstein (and probably wrong) code?
Thanks
--
Posted via http://www.ruby-forum.com/\.