#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    3
    Rep Power
    0

    Perl Substitution not working through script.


    I have to substitute:

    VT_HOST => [
    192.168.55.66,
    ],

    with
    VT_HOST =>[111.222.333.444,],

    through script.

    I am using following command in the script.

    /usr/bin/perl -p -i -e "s/\s\+\'VT_HOSTS\'\s\+=>\s\+\[\n\s\+\'\d\+\.\d\+\.\d\+\.\d\+\'\,\n\s\+\]\,/\ \ \ \'VT_HOSTS\'\ \ \ \ =>\ \[\ \'\192\.\166\.\11\.\22\'\,\]\,/g" /home/xxprod/JK/12Dec/filed.pm


    But it is not working.
    The same Regex when I run through vi editorusing :%s/originaltest/new_test/g is working.


    Can anybody solve this issue...why it is happening.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    835
    Rep Power
    496
    There may be some other issues in you regex, but you should at least remove the backslahes (\) that appear right before the plus (+) signs.

    /\d+/ : looks for one or several digits
    /\s\+/ : looks for one digit followed by the + sign.
  4. #3
  5. kill 9, $$;
    Devshed Supreme Being (6500+ posts)

    Join Date
    Sep 2001
    Location
    Shanghai, An tSín
    Posts
    6,897
    Rep Power
    3887
    You have VT_HOSTS in your regexp, though it's VT_HOST in your data.

    However, this isn't going to work anyway. The -p switch causes perl to read the file line-by-line (printing each line after it has been processed). However, your data spans three lines, so it won't all be in $_ at the same time. Therefore your regexp won't match.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    3
    Rep Power
    0
    Hi,

    This type of logic will work your problem :

    @input = ("VT_HOST => [","192.168.55.66,","],");
    foreach(@input)
    {
    chomp;
    $input.=$_
    }
    print "\nValue : $input";
    $input=~s/(\d{3}.\d{3}.\d{2}.\d{2},)/111.222.333.444,/g;
    print "\nValue : $input";
  8. #5
  9. kill 9, $$;
    Devshed Supreme Being (6500+ posts)

    Join Date
    Sep 2001
    Location
    Shanghai, An tSín
    Posts
    6,897
    Rep Power
    3887
    Originally Posted by Rahul6990
    Hi,

    This type of logic will work your problem :

    @input = ("VT_HOST => [","192.168.55.66,","],");
    foreach(@input)
    {
    chomp;
    $input.=$_
    }
    print "\nValue : $input";
    $input=~s/(\d{3}.\d{3}.\d{2}.\d{2},)/111.222.333.444,/g;
    print "\nValue : $input";
    This assumes that there's nothing else in the file that looks like an IP address. Otherwise all of them will be replaced.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    3
    Rep Power
    0
    Originally Posted by ishnid
    This assumes that there's nothing else in the file that looks like an IP address. Otherwise all of them will be replaced.
    Code:
    $input=~s/(\d{3}.\d{3}.\d{2}.\d{2},)/111.222.333.444,/g;
    line will make sure that script is substituting IP address only. not everything.
  12. #7
  13. kill 9, $$;
    Devshed Supreme Being (6500+ posts)

    Join Date
    Sep 2001
    Location
    Shanghai, An tSín
    Posts
    6,897
    Rep Power
    3887
    Originally Posted by Rahul6990
    Code:
    $input=~s/(\d{3}.\d{3}.\d{2}.\d{2},)/111.222.333.444,/g;
    line will make sure that script is substituting IP address only. not everything.
    Yes, but my point is that the code doesn't check if this follows VT_HOST, so anything that looks like an IP address will be replaced. The OP only wishes to replace the one related to VT_HOST.

IMN logo majestic logo threadwatch logo seochat tools logo