Hello, I am trying to implement the ruby split method to pull out
strings from the above chunk of data and then assign them to variables.
For each DBINSTANCE line I need the 2nd and 4th space delimited strings,
which I then will use to fill out a full hash. My extra credit to
myself is to only pull string from the DBINSTANCE with the SECGROUP
monitoring.
From the look of that text, you could start by using split on two
consecutive newlines “\n\n”, to isolate each “DBINSTANCE” section. Then
you could use split on “\n” to break down the lines, and then split on
spaces / +/, and then select the appropriate elements in the nested
Arrays based on their indices, or by using “select”
Regular Expressions would also be a viable way to do this.
DBINSTANCE prod-ro-db8-v75 mysql 400 available general-public-license
“occurances” => 1,
“subscribers” => [“urls”]
}
]}.to_json
Hello, I am trying to implement the ruby split method to pull out
strings from the above chunk of data and then assign them to variables.
For each DBINSTANCE line I need the 2nd and 4th space delimited strings,
which I then will use to fill out a full hash. My extra credit to
myself is to only pull string from the DBINSTANCE with the SECGROUP
monitoring.
You can use this as a start
File.read(“x”).scan /DBINSTANCE\s+(.*?)(?=DBINSTANCE|\z)/m do |match|
match = match.first
if /SECGROUP/ =~ match
strings = match.split /\s+/
p strings
end
end
FYI regarding the “extra credit”. Using the above structure with map,
you could use a conditional to test whether there is more than one line
in that section, whether “monitoring” is in the appropriate position,
return nil when it is not there, and then compact the result.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.