0

the perl one liner command to replace Value in file is simple:

perl  -i  's/oldval/newval/g' file.txt

but what should be the perl one liner syntax in case we want to replace the value on specific parameter ( value is after "=" seperator

Example

We want to match the VIP_IP string and replace the value after "=" separator with other value as 192.17.200.100 ( with perl one liner syntax )

more file.txt

VIP_IP=172.17.200.15

if [[ $VIP_IP = $HOST_IP ]]
then

.
.
.

Expected results

more file.txt

VIP_IP=192.17.200.100

if [[ $VIP_IP = $HOST_IP ]]
then

.
.
.

1 Answer 1

2

Include the key in your regexp.

If you know exactly how it is written, then you can use a positive lookbehind assertion and it's easier:

perl -pi -e 's/(?<=^\$VIP_IP=).*/192.17.200.100/' your_file

Otherwise, you must include the matched key in the replacement text using $1:

perl -pi -e 's/(^\$VIP_IP\s*=).*/$1192.17.200.100/' your_file

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .