Extract a range i.e. svr[100..130] ?

Discussion in 'Ruby' started by Richard Sandoval, Apr 15, 2011.

  1. What best method could extract the range of a given list of servers?

    I have a field name on a UI that contains a list of servers and it can
    be a range such as
    svr[100..130].domain.local,prod[10..13].otherdomain.local.


    What would be my best approach to single each one of those nodes out to
    iterate through?

    --
    Posted via http://www.ruby-forum.com/.
    Richard Sandoval, Apr 15, 2011
    #1
    1. Advertising

  2. On Fri, Apr 15, 2011 at 6:08 PM, Richard Sandoval <> wrote:
    > What best method could extract the range of a given list of servers?
    >
    > I have a field name on a UI that contains a list of servers and it can
    > be a range such as
    > svr[100..130].domain.local,prod[10..13].otherdomain.local.
    >
    >
    > What would be my best approach to single each one of those nodes out to
    > iterate through?


    Sorry, if I didn't understand this well. You have a string containing
    "svr[100..130].domain.local" and you want:

    svr100.domain.local
    svr101.domain.local
    ....
    svr130.domain.local

    ?

    If that's the case, then this might work:

    a = "svr[100..130].domain.local"
    m = a.match(/(.*?)\[(\d+)\.\.(\d+)\](.*)/)
    (m[2].to_i..m[3].to_i).each {|num| puts "#{m[1]}#{num}#{m[4]}"}

    =>
    svr100.domain.local
    svr101.domain.local
    svr102.domain.local
    [...snip...]
    svr129.domain.local
    svr130.domain.local

    Jesus
    Jesús Gabriel y Galán, Apr 15, 2011
    #2
    1. Advertising

  3. Richard Sandoval

    7stud -- Guest

    Re: Extract a range i.e. svr?

    "Jes=C3=BAs Gabriel y Gal=C3=A1n" <> wrote in pos=
    t =

    #993035:
    > Sorry, if I didn't understand this well. You have a string containing
    > "svr[100..130].domain.local" and you want:
    >
    > svr100.domain.local
    > svr101.domain.local
    > ....
    > svr130.domain.local
    >
    > ?
    >
    > If that's the case, then this might work:
    >
    > a =3D "svr[100..130].domain.local"
    > m =3D a.match(/(.*?)\[(\d+)\.\.(\d+)\](.*)/)
    > (m[2].to_i..m[3].to_i).each {|num| puts "#{m[1]}#{num}#{m[4]}"}
    >


    Here's my version:

    str =3D "svr[100..130].domain.local"

    range_pattern =3D /
    \[ #a literal opening bracket
    (\d+) #capture a series of one or more digits
    [.]{2} #two literal periods
    (\d+) #capture a series of one or more digits
    \] #a literal closing bracket
    /xms

    before_range, the_range, after_range =3D str.partition(range_pattern)
    start_range, end_range =3D $1, $2

    start_range.upto(end_range) do |i|
    puts "#{before_range}#{i}#{after_range}"
    end

    --output:--
    svr100.domain.local
    svr101.domain.local
    svr102.domain.local
    svr103.domain.local
    svr104.domain.local
    svr105.domain.local
    svr106.domain.local
    svr107.domain.local
    svr108.domain.local
    svr109.domain.local
    svr110.domain.local
    svr111.domain.local
    svr112.domain.local
    svr113.domain.local
    svr114.domain.local
    svr115.domain.local
    svr116.domain.local
    svr117.domain.local
    svr118.domain.local
    svr119.domain.local
    svr120.domain.local
    svr121.domain.local
    svr122.domain.local
    svr123.domain.local
    svr124.domain.local
    svr125.domain.local
    svr126.domain.local
    svr127.domain.local
    svr128.domain.local
    svr129.domain.local
    svr130.domain.local

    -- =

    Posted via http://www.ruby-forum.com/.=
    7stud --, Apr 15, 2011
    #3
  4. This is a definite step in the right direction and I appreciate your
    assistance Jesus.

    So I have a fieldname in a UI which is named Hostnames:

    Within the hostname field, it could have a single host named
    svr10.domain.local or it could have a range like
    svr.10.domain.local,svr[100..103].domain.local.

    Essentially what I am trying to do is to get that hostname field in my
    script and for each individual host and/or a range of hosts then do a
    specific command or go through my work flow.

    --
    Posted via http://www.ruby-forum.com/.
    Richard Sandoval, Apr 15, 2011
    #4
  5. Richard Sandoval

    7stud -- Guest

    Richard Sandoval wrote in post #993057:
    > This is a definite step in the right direction and I appreciate your
    > assistance Jesus/7stud
    >
    > So I have a fieldname in a UI which is named Hostnames:
    >
    > Within the hostname field, it could have a single host named
    > svr10.domain.local or it could have a range like
    > svr.10.domain.local,svr[100..103].domain.local.
    >
    > Essentially what I am trying to do is to get that hostname field in my
    > script and for each individual host and/or a range of hosts then do a
    > specific command or go through my work flow.


    ...and so what have you tried given the above?

    --
    Posted via http://www.ruby-forum.com/.
    7stud --, Apr 15, 2011
    #5
  6. Richard Sandoval, Apr 15, 2011
    #6
  7. Richard Sandoval, Apr 15, 2011
    #7
  8. 7stud,

    you use upto but that doesnt work for 1.8.6, what method could I use in
    this scenario?

    --
    Posted via http://www.ruby-forum.com/.
    Richard Sandoval, Apr 15, 2011
    #8
  9. On Fri, Apr 15, 2011 at 8:40 PM, Richard Sandoval <> wrote:
    > undefined method `match' for ["svr[100..103].domain.local"]:Array
    > (NoMethodError)


    This is because you don't have a string, you have an array. If you
    have that value for example in params[:hostname], try this:

    a = params[:hostname].first

    and then the rest of my solution.

    Jesus.
    Jesús Gabriel y Galán, Apr 15, 2011
    #9
  10. Hi jesus, svr[100..103].domain.local would be one of the keys in the
    array.

    My array looks like

    ["svr10.domain.local", "svr[100-103].domain.local",
    "svr[200-300].domain.local"]

    --
    Posted via http://www.ruby-forum.com/.
    Richard Sandoval, Apr 15, 2011
    #10
  11. Here is what i am trying to do.



    nodes = cfv.values.to_s.split(/[, \n]+/)
    a = nodes
    m = a.match(/(.*?)\[(\d+)\.\.(\d+)\](.*)/)

    puts "nodes: #{m.inspect}"


    m.each do |node|
    puts "checking to see if #{node} exists"

    --
    Posted via http://www.ruby-forum.com/.
    Richard Sandoval, Apr 15, 2011
    #11
  12. On Fri, Apr 15, 2011 at 11:54 PM, Richard Sandoval <> wrote:
    > Here is what i am trying to do.
    >
    >
    >
    > nodes = cfv.values.to_s.split(/[, \n]+/)


    split returns an array
    (http://ruby-doc.org/core-1.8.7/classes/String.html#M000776). So you
    can do the following:

    nodes.each do |node|
    m = node.match(/(.*?)\[(\d+)\.\.(\d+)\](.*)/)
    if m
    (m[2].to_i..m[3].to_i).each {|num|
    do_something_with("#{m[1]}#{num}#{m[4]}")}
    else
    do_something_with(node)
    end
    end

    This will call do_something_with passing either each expanded server
    name or the original string if it doesn't match the regular
    expression.

    Jesus.
    Jesús Gabriel y Galán, Apr 15, 2011
    #12
  13. Richard Sandoval

    7stud -- Guest

    Richard Sandoval wrote in post #993091:
    > 7stud,
    >
    > you use upto but that doesnt work for 1.8.6, what method could I use in
    > this scenario?


    puts RUBY_VERSION

    1.upto(5) do |i|
    puts i
    end

    --output:--
    1.8.6
    1
    2
    3
    4
    5

    --
    Posted via http://www.ruby-forum.com/.
    7stud --, Apr 18, 2011
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. TomS
    Replies:
    0
    Views:
    371
  2. John A Grandy

    VS05 : switch VS Dev Svr to IIS

    John A Grandy, Mar 6, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    309
    John A Grandy
    Mar 6, 2006
  3. it_says_BALLS_on_your forehead

    extract range of lines using range op bug?

    it_says_BALLS_on_your forehead, Mar 3, 2006, in forum: Perl Misc
    Replies:
    3
    Views:
    179
    it_says_BALLS_on_your forehead
    Mar 3, 2006
  4. fred
    Replies:
    3
    Views:
    261
    Zifud
    Mar 17, 2005
  5. Replies:
    5
    Views:
    869
Loading...

Share This Page