Searching Text for Multiple Matches

Discussion in 'Ruby' started by jackster the jackle, Nov 30, 2009.

  1. I am using ruby to search and match text in a file that has several
    mathes.

    Here is the text:

    Ethernet adapter Local Area Connection:

    Connection-specific DNS Suffix . : corp.pc.com
    IP Address. . . . . . . . . . . . : 172.1.81.7
    Subnet Mask . . . . . . . . . . . : 255.255.255.0
    Default Gateway . . . . . . . . . : 172.1.81.254

    Ethernet adapter Network Connect Adapter:

    Media State . . . . . . . . . . . : Media disconnected

    Ethernet adapter Wireless Network Connection:

    Connection-specific DNS Suffix . : corp.pc.com
    IP Address. . . . . . . . . . . . : 172.1.17.116
    Subnet Mask . . . . . . . . . . . : 255.255.255.0
    Default Gateway . . . . . . . . . : 172.1.17.254

    What I need to do it pull out the IP address and default gateway IP
    addresses for each interface and store them for processing later on in
    the script.

    What method should I use to get the first ip address and gateway and
    store it, then grab the next set and store that?

    thanks
    --
    Posted via http://www.ruby-forum.com/.
    jackster the jackle, Nov 30, 2009
    #1
    1. Advertising

  2. jackster the jackle

    Roger Pack Guest


    > What method should I use to get the first ip address and gateway and
    > store it, then grab the next set and store that?


    I'd probably recommend a regular expression.
    something like
    a = big_string
    big_string =~ /something/



    or

    big_string.scan(/something/).each {|match|


    }
    --
    Posted via http://www.ruby-forum.com/.
    Roger Pack, Nov 30, 2009
    #2
    1. Advertising

  3. Thanks Roger. I am currently iterating through each line of text and
    matching on any lines that either have "IP Address" or "Default Gateway"
    in them and grabbing the IP addresses and assigning them to variables.

    The problem is, the first IP address I get seems to disapear toward the
    end of the script and I can't figure out why....perhaps because I'm
    using "elsif"?

    here is the code:

    ipconf = `ipconfig`

    ipconf.each do |x|

    if
    x[/IP\sAdd*/]
    x.scan(/\s*IP\sAddress.*\:\s([1-9]+\.[0-9]+\.[0-9]+\.[0-9]+)\s+$/)
    ip = $1
    puts "ip addr: " + $1.to_s ## THIS PRINTS OUT FINE


    elsif
    x[/Def*/]
    puts "XXXXX IP: #{ip}"
    x.scan(/\s*Default\sGateway.*\:\s([1-9]+\.[0-9]+\.[0-9]+\.[0-9]+)\s+$/)
    def_gate = $1
    puts "Default Gateway: "+$1.to_s ## THIS PRINTS OUT FINE


    puts "Print IP address again: #{ip_addr}" ### THIS HAS NO VALUE

    end
    end

    I think I need to somehow combine the "if" and "elsif" statements so I'm
    not using "or" and this way the ip_addr variable will hold it's value?

    thanks

    jack
    --
    Posted via http://www.ruby-forum.com/.
    jackster the jackle, Nov 30, 2009
    #3
  4. 2009/11/30 jackster the jackle <>:
    > Thanks Roger. I am currently iterating through each line of text and
    > matching on any lines that either have "IP Address" or "Default Gateway"
    > in them and grabbing the IP addresses and assigning them to variables.
    >
    > The problem is, the first IP address I get seems to disapear toward the
    > end of the script and I can't figure out why....perhaps because I'm
    > using "elsif"?
    >
    > here is the code:
    >
    > ipconf =3D `ipconfig`
    >
    > ipconf.each do |x|
    >
    > =A0 =A0 if
    > =A0 =A0 x[/IP\sAdd*/]
    > =A0 =A0 x.scan(/\s*IP\sAddress.*\:\s([1-9]+\.[0-9]+\.[0-9]+\.[0-9]+)\s+$/=

    )
    > =A0 =A0 ip =3D $1
    > =A0 =A0 puts "ip addr: " + $1.to_s =A0 ## THIS PRINTS OUT FINE
    >
    >
    > =A0 =A0 elsif
    > =A0 =A0 x[/Def*/]
    > =A0 =A0 puts "XXXXX IP: #{ip}"
    > =A0 =A0 x.scan(/\s*Default\sGateway.*\:\s([1-9]+\.[0-9]+\.[0-9]+\.[0-9]+)=

    \s+$/)
    > =A0 =A0 def_gate =3D $1
    > =A0 =A0 puts "Default Gateway: "+$1.to_s =A0 ## THIS PRINTS OUT FINE
    >
    >
    > =A0 =A0 puts "Print IP address again: #{ip_addr}" =A0### THIS HAS NO VALU=

    E
    >
    > =A0end
    > end
    >
    > I think I need to somehow combine the "if" and "elsif" statements so I'm
    > not using "or" and this way the ip_addr variable will hold it's value?


    I would attempt a two stage approach:

    1. stage: match a complete adapter description
    2. stage: match individual portions of it


    str =3D <<'XXX'
    Ethernet adapter Local Area Connection:

    Connection-specific DNS Suffix . : corp.pc.com
    IP Address. . . . . . . . . . . . : 172.1.81.7
    Subnet Mask . . . . . . . . . . . : 255.255.255.0
    Default Gateway . . . . . . . . . : 172.1.81.254

    Ethernet adapter Network Connect Adapter:

    Media State . . . . . . . . . . . : Media disconnected

    Ethernet adapter Wireless Network Connection:

    Connection-specific DNS Suffix . : corp.pc.com
    IP Address. . . . . . . . . . . . : 172.1.17.116
    Subnet Mask . . . . . . . . . . . : 255.255.255.0
    Default Gateway . . . . . . . . . : 172.1.17.254
    XXX

    str.scan %r{
    ^ \S [^:]* : \s* $
    (?:
    ^ \s+ [^:]+ : [^:]+ $ [\n\r]+
    )*
    }x do |adapter|
    # p adapter
    name =3D adapter[%r{Ethernet\s+adapter\s+(\S[^:]*):}, 1]
    addr =3D adapter[%r{IP Address[^:]*:\s*(\d+(?:\.\d+){3})}, 1]
    printf "%p %p\n", name, addr
    end

    Kind regards

    robert

    --=20
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
    Robert Klemme, Dec 2, 2009
    #4
    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. Replies:
    4
    Views:
    1,469
  2. Rajarshi Guha

    searching for matches within a word list

    Rajarshi Guha, Jul 29, 2003, in forum: Python
    Replies:
    1
    Views:
    285
    Scott David Daniels
    Jul 30, 2003
  3. Talin
    Replies:
    3
    Views:
    464
    Talin
    Nov 19, 2005
  4. jhu
    Replies:
    6
    Views:
    110
    Dave Weaver
    Nov 26, 2007
  5. stumblng.tumblr
    Replies:
    1
    Views:
    190
    stumblng.tumblr
    Feb 4, 2008
Loading...

Share This Page