Home > Enterprise >  Using sed with back-references calculated
Using sed with back-references calculated

Time:05-04

I would like

pm.max_children = 5

to become;

pm.max_children = 8

and have been working on it and got no soludtions. Backreferences like \1 or \2 don't seem to be working with calculation.

sed -i -E "s/(pm.max_children)[^=]*=\s*(. )/\1 = $(echo \2 3)/" /usr/local/etc/php-fpm.d/www.conf 

Using perl commands are OK as well as long as it is solved.

Thank you.

CodePudding user response:

perl -i.bak -wpe's/pm.max_children\s*=\s*\K([0-9] )/$1   3/e' file

This keeps a backup due to .bak; remove that (after testing) if you don't need a backup.

The \K drops all previous matches so they are not consumed and don't have to be restored in the replacement part.

CodePudding user response:

There are many ways, this is one of them:

perl -lpe 's/(?<= = )(\d)/${1} 3/e' <<< 'pm.max_children = 5'

for exact match:

perl -lpe 's/pm.max_children = \K(\d)/${1} 3/e' <<< 'pm.max_children = 5'

just match a number at the end:

perl -lpe 's/(\d)$/${1} 3/e' <<< 'pm.max_children = 5'

output:

pm.max_children = 8

For files, you can use -i in-place saving and instead of <<< (here document which is for testing) use a file.

perl -i -lpe '...' <FILE>

CodePudding user response:

Using sed

$ sed 's/\(.* \)\(.*\)/echo \1 \$((\2 3))/e' /usr/local/etc/php-fpm.d/www.conf
pm.max_children = 8
  • Related