简体   繁体   English

使用sed从php文件中删除注释

[英]remove comments from php file using sed

i'm trying to edit a php config file using sed in a bash script. 我试图在bash脚本中使用sed编辑php配置文件。 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. 但这在这种情况下不起作用。

I think your issue is simply that you're using double quotes, so $CFG is being expanded by the shell. 我认为您的问题很简单,因为您使用的是双引号,因此$CFG正在由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). 通常,我建议始终使用单引号,除非在极少数情况下,您将shell变量用作sed命令的一部分(它带有自己的陷阱)。

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. 要调试此类问题,请使用set -x ,它会向您显示您正在执行的命令与您打算使用的命令不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM