So I think this is probably what you are looking for though it was kind of hard to understand the question. Here's my solution and I'll go over each part.
hsh = Hash.new()
File.open('config.txt', 'r').each_line do |line|
match =
/WaTestProfile,
(?<unit>Unit\d+) # match the unit and unit number
,.* # match the comma and all the way up to the property
(?<property>AdminIPAddress|InterfaceDisplayString|Name) # match the property
\s+ # match all spaces up to the property value
{(?<property_value>.*)} # match the property value
/x =~ line
if match
hsh[unit.to_sym] ||= {}
hsh[unit.to_sym][property.to_sym] = property_value
end
end
hsh.each do |key, value|
puts key.to_s + value.to_s
end
So this is just creating a hash, we also could have done hsh = {}
hsh = Hash.new()
We then open the file for reading and read each line in. On each line we attempt to find a match. I've used a more advanced form of regex here using named captures and ignoring whitespace because it's a little more complicated, so it makes it easier to read
match =
/WaTestProfile,
(?<unit>Unit\d+) # match the unit and unit number
,.* # match the comma and all the way up to the property
(?<property>AdminIPAddress|InterfaceDisplayString|Name|PhysIf) # match the property
\s+ # match all spaces up to the property value
{(?<property_value>.*)} # match the property value
/x =~ line
Now I can reference the variable unit
and get the entire unit and unit number.
We also use the match =
at the start so that we can remove the DDOS lines from the equation.
Finally we check if there was a match. If there was we either create a new key corresponding to a hash in the top level hash or we grab an existing one. Then we set the property to the correct property value. I assumed since you didn't put quotes around the keys that you wanted them to be symbols, which is probably a better idea anyway.
if match
hsh[unit.to_sym] ||= {}
hsh[unit.to_sym][property.to_sym] = property_value
end
The final output looks like this:
Unit0{:AdminIPAddress=>"10.5.52.60", :InterfaceDisplayString=>"10_10", :Name=>"440_6PT_6AS_inf5"}
Unit1{:AdminIPAddress=>"10.5.52.60", :InterfaceDisplayString=>"8_8", :Name=>"440_6PT_6AS_inf4"}
Unit2{:AdminIPAddress=>"10.5.52.60", :InterfaceDisplayString=>"2_2", :Name=>"440_6PT_6AS_inf1"}
Unit3{:AdminIPAddress=>"10.5.52.60", :InterfaceDisplayString=>"4_4", :Name=>"440_6PT_6AS_inf2"}
Unit4{:AdminIPAddress=>"10.5.52.60", :InterfaceDisplayString=>"6_6", :Name=>"440_6PT_6AS_inf3"}
Unit5{:AdminIPAddress=>"10.5.52.60", :InterfaceDisplayString=>"0_0", :Name=>"440_6PT_6AS_inf0"}
All contained in the hash hsh
Psst:
If you want a shorter version, here it is:
hsh = {}
File.open('config.txt', 'r').each_line do |line|
if /WaTestProfile,(?<unit>Unit\d+),.*(?<property>AdminIPAddress|InterfaceDisplayString|Name)\s+{(?<property_value>.*)}/x =~ line
hsh[unit.to_sym] ||= {}
hsh[unit.to_sym][property.to_sym] = property_value
end
end
p hsh
Output:
{:Unit0=>{:AdminIPAddress=>"10.5.52.60", :InterfaceDisplayString=>"10_10", :Name=>"440_6PT_6AS_inf5"}, :Unit1=>{:AdminIPAddress=>"10.5.52.60", :InterfaceDisplayString=>"8_8", :Name=>"440_6PT_6AS_inf4"}, :Unit2=>{:AdminIPAddress=>"10.5.52.60", :InterfaceDisplayString=>"2_2", :Name=>"440_6PT_6AS_inf1"}, :Unit3=>{:AdminIPAddress=>"10.5.52.60", :InterfaceDisplayString=>"4_4", :Name=>"440_6PT_6AS_inf2"}, :Unit4=>{:AdminIPAddress=>"10.5.52.60", :InterfaceDisplayString=>"6_6", :Name=>"440_6PT_6AS_inf3"}, :Unit5=>{:AdminIPAddress=>"10.5.52.60", :InterfaceDisplayString=>"0_0", :Name=>"440_6PT_6AS_inf0"}}