gpt4 book ai didi

bash - 使用 sed 或 awk 注释掉一个文件 block

转载 作者:行者123 更新时间:2023-11-29 09:37:44 26 4
gpt4 key购买 nike

我想使用 sed 或 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;

在此,我想从代码的第二行注释掉以结束 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}'

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

最佳答案

I want to comment out from line two code to 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;
*/

关于bash - 使用 sed 或 awk 注释掉一个文件 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38460909/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com