Read file into an array1 then compare another array2 to it and pull info out

Hi,
New to ruby but have used it alot in the last year to ssh into multiple
servers to run commands and ftp files to remote servers as well.
Current issue is that I am read a file into an array which I have done.
Now I want to pull information out of the array by comparing it to
another array.
for each item in the list array, I would like to pull the line out of
the lines array.

#!/usr/bin/env ruby
require ‘io/console’

list = [‘host_name’, ‘service_description’, ‘active_checks_enabled’,
‘notifications_enabled’]
lines = IO.readlines("/home/download/status.dat")
puts lines.length
lines.each do |line|
if line == list
puts line
end
end

Tim Roush wrote in post #1170261:

lines.each do |line|
if line == list
puts line
end
end

lines.each do |line|
puts line if list.any? {|a|a==line.chomp}
end

I try that but get no results but know that each one of items in list is
in the array lines.

Below is a example of what is in the file.
host_name=10.10.10.11
active_checks_enabled=1
notifications_enabled=1
service_description=Users_Total_OS_22980

#!/usr/bin/env ruby
require ‘io/console’

list = [‘host_name’, ‘service_description’, ‘active_checks_enabled’,
‘notifications_enabled’]
lines = IO.readlines("/home/download/status.dat")
puts lines.length
lines.each do |line|
puts line if list.any? {|a|a==line.chomp}
end

Tim Roush wrote in post #1170269:

I try that but get no results but know that each one of items in list is
in the array lines.

Below is a example of what is in the file.
host_name=10.10.10.11
active_checks_enabled=1
notifications_enabled=1
service_description=Users_Total_OS_22980

lines.each do |line|
field_name=line.chomp.split(’=’).first.strip
puts line if list.any? {|a|a==field_name}
end

ok, try it and receive the following error:

./rubyfile.rb:8:in block in <main>': undefined methodstrip’ for
nil:NilClass (NoMethodError)

Tim Roush wrote in post #1170274:

ok, try it and receive the following error:

./rubyfile.rb:8:in block in <main>': undefined methodstrip’ for
nil:NilClass (NoMethodError)

lines.each do |line|
if line =~ /^\s*(.+?)\s*=/
field_name=$1
puts line if list.any? {|a|a==field_name}
end
end

ok, that worked. How can I put this into an array?

puts line if list.any? {|a|a==field_name}

Can I put the above stdout into an array?

Tim Roush wrote in post #1170277:

ok, that worked. How can I put this into an array?

thank you
:slight_smile:

Tim Roush wrote in post #1170277:

ok, that worked. How …

thank you
:slight_smile:

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