0

I am trying to replace a string in the config file. I would like to run something like this:

OS

(docker image php:8.1-apache-buster)

Debian GNU/Linux 10 (buster)

sed (GNU sed) 4.7 Packaged by Debian

Possible inputs:

post_max_size = 4M
post_max_size = 24M
post_max_size = 248M
...

Example output (any user given value):

post_max_size = 128M

Example cmd:

sed -i 's/(post_max_size = ([0-9]{1,})M/post_max_size = 128M/g' /usr/local/etc/php/php.ini

enter image description here

Joining regex with strings does not work here.

It works when I run string replace without any regex

sed -i 's/post_max_size = 8M/post_max_size = 128M/g' /usr/local/etc/php/php.ini

This works only if the value of the post_max_size is set exactly to 2M. I would like to be able to make a change with regex regardless of the value set.

I searched the Internet and sed cmd docs but did not find anything which fits my use case.

3
  • Please replace image with its text. Commented Nov 13, 2022 at 9:35
  • Please add sample input (no descriptions, no images, no links) and your desired output for that sample input to your question (no comment). Commented Nov 13, 2022 at 9:36
  • 1
    Try sed -i -E '/post_max_size = /s/([0-9]{1,})M/128M/' /usr/local/etc/php/php.ini Commented Nov 13, 2022 at 10:31

2 Answers 2

2

The following should work:

sed -i 's/^post_max_size = .*/post_max_size = 128M/g' /usr/local/etc/php/php.ini
Sign up to request clarification or add additional context in comments.

Comments

1

You can match optional spaces with [[:space:]]*, the -E for extended-regexp and use group 1 noted as \1 followed by your replacement like \1128M

sed -E -i 's/(post_max_size[[:space:]]*=[[:space:]]*)[0-9]+M/\1128M/g' /usr/local/etc/php/php.ini

7 Comments

There is something missing here. I have tried this and similar commands, but it does not trigger string replacement. @C-nan cmd worked.
@DevWL What is your os?
its a dockeriesd container of php & Apache image "Debian GNU/Linux 10 (buster)"
@fpmmurphy command also worked: sed -i -E '/post_max_size = /s/([0-9]{1,})M/128M/' /usr/local/etc/php/php.ini
Yes this works also in my use case, Thanks
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.