Tomasz Kowalczyk
Tomasz Kowalczyk

Reputation: 1961

remove comments from php file using sed

i'm trying to edit a php config file using sed in a bash script. I'm stuck on removing comments from particular lines. I want to uncomment line:

// $CFG->phpunit_prefix = 'phpu_';

I tried command that worked for me to replace/update paths:

"s%// $CFG->phpunit_prefix%$CFG->phpunit_prefix%" config.php

But it doesn't work in this case.

Upvotes: 0

Views: 865

Answers (1)

Tom Fenech
Tom Fenech

Reputation: 74596

I think your issue is simply that you're using double quotes, so $CFG is being expanded by the shell. Change to single quotes:

sed 's%// $CFG->phpunit_prefix%$CFG->phpunit_prefix%' config.php

In general, I'd recommend always using single quotes except in the rare case that you're using a shell variable as part of a sed command (which comes with its own set of pitfalls).

For increased readability and to avoid repeating yourself, use a capturing group:

sed 's%// \($CFG->phpunit_prefix\)%\1%' config.php

To debug this kind of issue, use set -x, which will show you that the command you're executing is different to the one you intended to use.

Upvotes: 3

Related Questions