我正在尝试使用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/