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. Advertising

  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. Advertising

  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. 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. bob
    Replies:
    6
    Views:
    26,859
    G. G. Ditzel
    Nov 25, 2003
  2. Fangs
    Replies:
    3
    Views:
    9,802
    darshana
    Oct 26, 2008
  3. KBuser
    Replies:
    0
    Views:
    405
    KBuser
    Apr 5, 2006
  4. Aaron P Frenger

    Using schema to validate xml file segment

    Aaron P Frenger, Nov 4, 2004, in forum: XML
    Replies:
    0
    Views:
    509
    Aaron P Frenger
    Nov 4, 2004
  5. nass
    Replies:
    7
    Views:
    311
    Larry Smith
    Sep 26, 2006
Loading...

Share This Page