Should I use split?

DBINSTANCE prod-v55 mysql 300 available

DBINSTANCE prod-ro-db8-v55 mysql 400 available general-public-license
SECGROUP monitoring active
SECGROUP prd-security-group active

DBINSTANCE prod-ro-db8-v65 mysql 450 available general-public-license
SECGROUP monitoring active
SECGROUP prd-security-group active

DBINSTANCE prod-ro-db8-v75 mysql 400 available general-public-license
SECGROUP monitoring active
SECGROUP prd-security-group active

checkhash =
{ “checks” =>
[“mysql_disk_usage+#{foo1}” => {
“handlers” => [“default”, “mailer_alerts”],
“command” => “/etc/sensu/plugins/mysql-desk.rb -h -u -p
-s#{foo2}”,
“interval” => 300,
“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.

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.

On Tue, Oct 1, 2013 at 2:42 AM, jax jax [email protected] wrote:

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

Kind regards

robert

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.

| Privacy Policy | Terms of Service | Remote Ruby Jobs