On 2006年11月30日 22:24, amateur write:
On Thu, Nov 30, 2006 at 11:05:47AM +0800, Xie Yanbo wrote:
On 2006年11月30日 10:14, amateur write:
那是我没有表达清楚了,我的主要目的是去掉源代码中的注释,这个需要在多行
中进行搜索匹配,应该不是多行替换,因为 sed 的替换操作好似只能在一行内 使用,而 sed 的行操作 (d/p) 都是针对整行进行的,比如我用匹配 main 函数 的方法匹配注释的话,就会是的包含有 /* */ 注释的所有行都会被删除。象下
面的例子就不能用这种方法:

int main()
{
    do_something; /* this block is commented
    do_other_thing;
    ...
    do_third_thing; */

}
请看一下 remove_c_comment.sed 是否就是你要的脚本。
正是我要的功能,谢谢!
不过你发到我的个人邮箱里了,能否解释一下
        /\*\//!{ $!{ N; bt } }
的意思,看不懂其中 !{ $!{ N; bt } } 这个操作的意义
这行是查找C注释块的结束标识“*/”,如果没有找到它,并且也不是文件的最后一 行,那么就追加读入下一行,并回到这个循环的最开始位置,继续处理。
0$ cat remove_c_comment.sed
:t
/\/\*/,/\*\// {
/\*\//!{ $!{ N; bt } }
s/\/\*.*\*\///;
}
0$ cat test.c
int main()
{
   do_something; /* this block is commented
   do_other_thing;
   ...
   do_third_thing; */
   return 0;
}
0$ cat test.c | sed -f remove_c_comment.sed
int main()
{
   do_something;
   return 0;
}
0$

-------------------------



--
To UNSUBSCRIBE, email to [EMAIL PROTECTED]
with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]

回复