From: Wayne Brissette Date: 2012-07-17T10:43:38+09:00 Subject: Re: extracting part of a string I'm sure somebody will come up with a one-liner (they always do). ;) But I would do this: ruby-1.9.3-p125 :001 > server_names = %w{server.corp.abc.com:/vol/test/xyz, server2.corp.def.com:/vol/stage/123, server3.corp.xyz.net:/vol/prod/abc} => ["server.corp.abc.com:/vol/test/xyz,", "server2.corp.def.com:/vol/stage/123,", "server3.corp.xyz.net:/vol/prod/abc"] ruby-1.9.3-p125 :002 > server_names.each do | name | ruby-1.9.3-p125 :003 > servername = name.split(":") ruby-1.9.3-p125 :004?> print "#{servername[0]}\n" ruby-1.9.3-p125 :005?> end server.corp.abc.com server2.corp.def.com server3.corp.xyz.net => ["server.corp.abc.com:/vol/test/xyz,", "server2.corp.def.com:/vol/stage/123,", "server3.corp.xyz.net:/vol/prod/abc"] Wayne On Jul 16, 2012, at 8:20 PM, Jon R. wrote: > I have a file of the form > > server.corp.abc.com:/vol/test/xyz > server2.corp.def.com:/vol/stage/123 > server3.corp.xyz.net:/vol/prod/abc > > and etc. > > How do I return a list of only the server names? In other words > everything up to but not including the ":". I can read the file in and > can even split it at the ":" but can't seem to figure out how to only > get server.corp.abc.com > server2.corp.def.com > server3.corp.xyz.net > > Any help is appreciated. > Thanks, > Jon > > -- > Posted via http://www.ruby-forum.com/. >