user1914693
user1914693

Reputation: 21

Reading input from text file into an array in ruby

I want to read data from a text file and retrieve just the ip_addresses contained in the file.

The file contains data as:

[#<host:192.168.2.135>, #<host:175.41.142.49>, #<host:20:aa:4b:9b:eb:8c>, #<host:20:7c:8f:10:38:a0>, #<host:192.168.2.1>, #<host:239.255.255.250>]

From these data, i just want the host addresses like 192.168.2.135 and so on and store them in an array.Could somebody please help me with this? Thanks in advance.

Upvotes: 0

Views: 462

Answers (1)

sawa
sawa

Reputation: 168101

open(path_to_file, &:read).scan(/#<host:(.+?)>/).flatten

Upvotes: 2

Related Questions