我想删除评论中的标签,例如此示例;
<?php
//this is a comment with tab
$tes = 1;
$this->HTTP_URL = str_replace('///', '//', $this->http.$this->serverName.$this->port.$_SERVER['PHP_SELF']);
if ($tes == 1) {
//this is a comment with tab
echo $tes;
}
//this is a comment with tab
$tes = 2;
我用它测试了它;
sed -e's | / *。 * / || g'-e's | //. || g'test.php&gt; test2.php
但结果是;
this is a comment with tab
$tes = 1;
', $this->http.$this->serverName.$this->port.$_SERVER['PHP_SELF']);
if ($tes == 1) {
this is a comment with tab
echo $tes;
}
this is a comment with tab
$tes = 2;
我希望它看起来像;
//this is a comment with tab
$tes = 1;
$this->HTTP_URL = str_replace('///', '//', $this->http.$this->serverName.$this->port.$_SERVER['PHP_SELF']);
if ($tes == 1) {
//this is a comment with tab
echo $tes;
}
//this is a comment with tab
$tes = 2;
甚至更好
/* this is a comment with tab */
$tes = 1;
$this->HTTP_URL = str_replace('///', '//', $this->http.$this->serverName.$this->port.$_SERVER['PHP_SELF']);
if ($tes == 1) {
/* this is a comment with tab */
echo $tes;
}
/* this is a comment with tab */
$tes = 2;
我尝试过各种各样的方式,但仍未达到我想要的结果。 谢谢。
...
我试过
gsed -i 's|^\s*//|//|' test.php
然后
gsed -i ':a;$!{N;ba};s/^/\x00/;tb;:b;s/\x00$//;t;s/\x00\(\/\*[^*]*\*\+\([^/*][^*]*\*\+\)*\/\)/\1\x00/;tb;s/\x00\/\/\([^\n]*\)/\/*\1\*\/\x00/;tb;s/\x00\(.\)/\1\x00/;tb' test.php
但结果是
<?php
/*this is a comment with tab*/
$tes = 1;
$this->HTTP_URL = str_replace('/*/', '//', $this->http.$this->serverName.$this->port.$_SERVER['PHP_SELF']);*/
if ($tes == 1) {
/*this is a comment with tab*/
echo $tes;
}
/*this is a comment with tab*/
$tes = 2;
注意错误第4行
$this->HTTP_URL = str_replace('/*/', '//', $this->http.$this->serverName.$this->port.$_SERVER['PHP_SELF']);*/
不应该更换。需要另一种语法
答案 0 :(得分:1)
sed '/^[[:space:]]*\/\//{s/^[[:space:]]*//}' yourfile.php
应该这样做。如果您想进行infile编辑,请执行
sed -i~ '/^[[:space:]]*\/\//{s/^[[:space:]]*//}' yourfile.php
将创建以〜开头的原始文件的备份。
答案 1 :(得分:1)
这可能适合你(GNU sed):
sed -i 's|^\s*//|//|' file
这将从前两个非空格字符为//
的行的开头删除零个或多个空格或制表符(空格)。
如果只是要移除的标签:
sed -i s|^\t*//|//|' file
N.B。这可以替代什么都没有,即以//
开头的行仍然会影响替换,所以最后的解决方案应该是:
sed -i 's|^\t\+//|//|' file
编辑:
sed -i 's|^\t\+//\(.*\)$|/*\1*/' file
或者更改//
,无论如何:
sed -i 's|^\t\+//|//|;s|//\(.*\)|/*\1*/|' file