I have a text file similar to this on a remote device:
D0-23-DB-31-04-3E%20192.168.4.42%20dnat
68-A8-6D-0C-38-B2%20192.168.4.40%20dnat
I created a small Ruby script to convert this to a string and post it to my Rails application:
def chilli_list
hash = File.open("/tmp/client_list", "rb").read
return hash
end
The output then looks like:
"D0-23-DB-31-04-3E%20192.168.4.42%20dnat\n68-A8-6D-0C-38-B2%20192.168.4.40%20dnat\n"
I need to extract the information bit by bit and display in a view. So far, I've not got far. I tried the following which is OK:
str = "D0-23-DB-31-04-3E%20192.168.4.42%20dnat\n68-A8-6D-0C-38-B2%20192.168.4.40%20dpass\n"
str.each_line do |line|
puts line.split(" ").first
puts line.split(" ").second
end
Is that the best way to do it or is there a better way?
Finally and most importantly, I also need to perform a few calculations on the string. I can count the lines with str.lines.count, but what I need is a count of the lines where the third value == nat as in the example above.
How can I go about that?