toripiyo
toripiyo

Reputation: 13

How to avoid wrong variable name interpretation in perl one-liner

I want to replace apache configuration file's MinSpareThreads directive value from 75 to 125.

I wrote perl one-liner code like below.

perl -pi.$(date +%Y%m%d) -e 's;MinSpareThreads(\s+)(\d+);MinSpareThreads$1125;g' httpd-mpm.conf

However it outputs below result.

root@8c659a9d5907:/usr/local/apache2/conf/extra# diff httpd-mpm.conf.20180130 httpd-mpm.conf
46c46
<     MinSpareThreads         75
---
>     MinSpareThreads
63c63
<     MinSpareThreads         75
---
>     MinSpareThreads
83c83
<     MinSpareThreads         25
---
>     MinSpareThreads
97c97
<     MinSpareThreads          5
---
>     MinSpareThreads

It seems $1 is unintendedly recognized as $1125. $1125 doesn't exist. So above result is shown.

Do you know any way to avoid above wrong interpretation?

Upvotes: 0

Views: 110

Answers (1)

Dave Cross
Dave Cross

Reputation: 69314

In order to use a Perl variable in a context where its name won't be distinguished from the surrounding text, you can put the name (not including the sigil) inside { ... }. So, instead of having $1125, you would have ${1}125.

Upvotes: 4

Related Questions