1

I'd like to removed a semicolon from one of these lines in the php.ini using bash shell command (perhaps using sed). How would I do that?

;extension =php_curl.dll    
 ; extension=php_gd2.dll
;extension = php_mbstring.dll
;extension= php_openssl.dll

I intentionally added spaces here and there.

What I've tried:

sed -e "s/^;extension\s=\sphp_gd2\.dll$/extension=php_gd2\.dll/" -i php.ini

This just places everyting into a single line:

;extension =php_curl.dll ; extension=php_gd2.dll;extension = php_mbstring.dll;extension= php_openssl.dll
NinjaFart
  • 1,626
  • 1
  • 20
  • 24

1 Answers1

1

This removes a semicolon at the beginning of a line that has one or more spaces on both sides

sed '/php_gd2/s/^\s\s*;\s\s*//' php.ini

You can specify which php extension you want to operate on by putting it in between slashes at the beginning. Then it will run the substitute command only on the lines that have the specified extension

To be even more selective,

sed '/^\s*;\s*extension\s*=\s*php_gd2.*\.dll/s/^\s\s*;\s\s*//' php.ini

ramana_k
  • 1,933
  • 2
  • 10
  • 14
  • Perhaps I was not too clear. I'd like to specify which php extension I want to enable. – NinjaFart Nov 21 '15 at 21:17
  • Great! But shouldn't I check if the line starts with `; extension` and ends with `.dll` in case the text is inside e.g. a comment. – NinjaFart Nov 21 '15 at 21:29
  • That can be done with `sed '/extension\s*=\s*php_gd2.*\.dll/s/^\s\s*;\s\s*//' php.ini` – ramana_k Nov 21 '15 at 21:35
  • Superb! Thanks Thomas – NinjaFart Nov 21 '15 at 21:40
  • `sed -b '/^\s*;\s*extension\s*=\s*php_gd2\.dll/s/^\s\s*;\s\s*//' -i php.ini` - Added `-b` because of [line ending issue on Windows](http://stackoverflow.com/a/11508669/1772200). Also added `-i` to make changes on the file instead of just printing it out. – NinjaFart Nov 21 '15 at 21:52