need to replace one segment of an IP

Discussion in 'Perl Misc' started by kreiss, Aug 6, 2004.

  1. kreiss

    kreiss Guest

    for an admin script, I would like to replace the fourth octet of
    an IP, ie: 192.168.0.10 would become 192.168.0.20.

    there must be an easy way to do this with a scalar that I am just too
    dense to find myself. any help would be appreciated.
     
    kreiss, Aug 6, 2004
    #1
    1. Advertisements

  2. kreiss <> writes:

    > for an admin script, I would like to replace the fourth octet of
    > an IP, ie: 192.168.0.10 would become 192.168.0.20.
    >
    > there must be an easy way to do this with a scalar that I am just too
    > dense to find myself. any help would be appreciated.


    If you have the IP address as a string in $ip and the last octet in a
    variable called $hostnum, you could simply do

    $ip =~ s/\d+$/$hostnum/;

    -----ScottG.
     
    Scott W Gifford, Aug 6, 2004
    #2
    1. Advertisements

  3. kreiss wrote:

    > for an admin script, I would like to replace the fourth octet of
    > an IP, ie: 192.168.0.10 would become 192.168.0.20.
    >
    > there must be an easy way to do this with a scalar that I am just too
    > dense to find myself. any help would be appreciated.


    Your problem is that you've not analysed your problem. The description
    you've given is totally inadequate for anyone to figure out what you
    actually want to do. Before you can implement anything in any language
    you need first to be able to describe what it is you want to do.

    Random shot in the dark: You want to add 10 to the last element any
    dotted quad in a string.

    s/(\d+\.\d+\.\d+\.)(\d+)/$1.($2+10)/eg;

    I've assumed here that we don't care what happen to longer dotted
    sequences of digits.
     
    Brian McCauley, Aug 6, 2004
    #3
  4. kreiss

    Matt Garrish Guest

    "kreiss" <> wrote in message
    news:p...
    > for an admin script, I would like to replace the fourth octet of
    > an IP, ie: 192.168.0.10 would become 192.168.0.20.
    >


    In addition to the regex approaches already provided, you can also split and
    recombine to manipulate the parts (although far more work than needed in
    this case):

    my $ip = '192.168.0.10';

    my @octet = split(/\./, $ip);

    # whatever quick and dirty manipulation/checks
    $octet[3] = '20';

    $ip = join('.', @octet);

    Matt
     
    Matt Garrish, Aug 7, 2004
    #4
  5. kreiss

    kreiss Guest

    On Fri, 06 Aug 2004 12:43:46 -0400, Scott W Gifford wrote:

    > kreiss <> writes:
    >
    >> for an admin script, I would like to replace the fourth octet of
    >> an IP, ie: 192.168.0.10 would become 192.168.0.20.
    >>
    >> there must be an easy way to do this with a scalar that I am just too
    >> dense to find myself. any help would be appreciated.

    >
    > If you have the IP address as a string in $ip and the last octet in a
    > variable called $hostnum, you could simply do
    >
    > $ip =~ s/\d+$/$hostnum/;
    >
    > -----ScottG.

    that is great, thannks to all who *helped*.
     
    kreiss, Aug 7, 2004
    #5
    1. Advertisements

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. news.frontiernet.net
    Replies:
    6
    Views:
    1,448
    news.frontiernet.net
    Apr 16, 2004
  2. loveNUNO
    Replies:
    2
    Views:
    1,217
    loveNUNO
    Nov 20, 2003
  3. Brian Blais
    Replies:
    1
    Views:
    588
    Bruno Desthuilliers
    Jun 27, 2006
  4. Greg Ewing
    Replies:
    2
    Views:
    547
    Dieter Maurer
    Jun 29, 2006
  5. nass
    Replies:
    7
    Views:
    419
    Larry Smith
    Sep 26, 2006
  6. Alun
    Replies:
    3
    Views:
    5,337
    Masudur
    Feb 18, 2008
  7. Merciadri Luca
    Replies:
    4
    Views:
    1,063
  8. Prasad S
    Replies:
    2
    Views:
    416
    Dr John Stockton
    Aug 27, 2004
Loading...