vasista2k has asked for the wisdom of the Perl Monks concerning the following question:


Hi

I want to replace an IP from a file using perl one liner.
Eg:
The file test.sh has before changing


export SERVING_GW_ADDR=47.104.153.103
export TEMP_SGW_PDP_ADDR=47.104.153.103

I want below result once the command is executed



export SERVING_GW_ADDR=47.104.153.103
export TEMP_SGW_PDP_ADDR=47.104.153.163

In the bove example 47.104.153.103 may not be same always for the variable TEMP_SGW_PDP_ADDR. But the output should be same.


Regards
Vasistha

Replies are listed 'Best First'.
Re: perl one line helper
by ph0enix (Friar) on Dec 07, 2009 at 15:34 UTC
    perl -ne 's/TEMP_SGW_PDP_ADDR=.*$/TEMP_SGW_PDP_ADDR=47.104.153.163/; print $_' < test.sh

      Using the -p switch and a positive look-behind will save some typing. I also prefer the safety net of in-place editing with the -i switch.

      $ cat test.sh some stuff export SERVING_GW_ADDR=47.104.153.103 export TEMP_SGW_PDP_ADDR=47.104.153.103 more stuff $ perl -pi.BAK -e ' > s/(?<=TEMP_SGW_PDP_ADDR=).*$/47.104.153.163/;' test.sh $ head -99 test.sh* ==> test.sh <== some stuff export SERVING_GW_ADDR=47.104.153.103 export TEMP_SGW_PDP_ADDR=47.104.153.163 more stuff ==> test.sh.BAK <== some stuff export SERVING_GW_ADDR=47.104.153.103 export TEMP_SGW_PDP_ADDR=47.104.153.103 more stuff $

      I hope this of interest.

      Cheers,

      JohnGG

      did you heard about switch -p :-) ?
      Little bit improved version:
      perl -pe 's/(?<=TEMP_SGW_PDP_ADDR=).*$/47.104.153.163/'
        For more information about -p see perlrun.

        print+qq(\L@{[ref\&@]}@{['@'x7^'!#2/"!4']});