使用sed或awk注释掉文件块 [英] Comment out a block a file using sed or awk

查看:53
本文介绍了使用sed或awk注释掉文件块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用sed或awk注释掉文件中的一段代码.

I would like to comment out a block of code in a file using sed or awk.

例如输入

this is source file;
line one code;
line two code;
line three code;
line four code;
line 5 code;
if something then
  line 6 code;
end if;

在这里,我想从代码的第二行中注释掉,如果结束.

In this, I want to comment out from line two of code to end if.

即输出应为

this is source file;
line one code;
/*
line two code;
line three code;
line four code;
line 5 code;
if something then
  line 6 code;
end if;
*/

尝试过此

awk '"/line two code;/{e=0}/end if;/" {printf("%s%s%s\n", "/*", $0, "*/"); next} {print}'

但是,它在每行代码之间附加了/*和*/.

But, it appends /* and */ between each line of code.

推荐答案

我想从第二行代码注释到 end if :

您可以像这样使用 awk :

awk '/line two code;/{print "/*"; p++} 1; p && /end if;/{print "*/"; p=0}' file

this is source file;
line one code;
/*
line two code;
line three code;
line four code;
line 5 code;
if something then
  line 6 code;
end if;
*/

这篇关于使用sed或awk注释掉文件块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆