gpt4 book ai didi

linux - 从代码中删除多余的大括号

转载 作者:太空宇宙 更新时间:2023-11-04 03:37:34 26 4
gpt4 key购买 nike

我正在尝试使用tr命令或任何类似的命令来对此 block 执行特定的文本操作:

if (/*condition*/)
{
statement1;
}
int a=3;
if (a)
{
statement1;
statement2;
}
else
{
statement1;
statement2;
statement3;
statement4;
///may be more lines
}

我想删除单行命令并得到这个:

if (/*condition*/)
statement1;
int a=3;
if (a)
{
statement1;
statement2;
}
else
{
statement1;
statement2;
statement3;
statement4;
///may be more lines
}

我已经尝试过tr -s '{}' ' ' <file.txt但它会挤压所有大括号,而不是使用特定的单行格式。

谢谢

最佳答案

您可以将记录分隔符设置为 {} 并检查您有多少个字段。如果有多个,请在周围打印括号:

$ awk -v RS=[{}] 'NF>1{$0="{"$0"}"}1' file
if(true)


statement;


if(false)

{
statement1;
statement2;
}

关于linux - 从代码中删除多余的大括号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31070714/

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