0

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.

7
  • If you want to strip out comments (why do you need to) then I'd suggest using NikiC's PHP parser rather than sed Commented Nov 9, 2015 at 10:22
  • @Mark you mean you don't wanna use the horrendous token_get_all() and friends directly? :) Commented Nov 9, 2015 at 10:30
  • I'm stripping comments using a bash script, that is being executed by Continuous Integration server Commented Nov 9, 2015 at 10:30
  • Sounds like a job for an environment-specific config file, ideally. Commented Nov 9, 2015 at 10:31
  • 1
    @TomFenech - Not if I can help it; but NikiC's parser is actually very good, and handles recreating the modified file again Commented Nov 9, 2015 at 10:32

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.