[英]How can I make multiple lines into one line using bash?
所以我有这样的代码:
else if(between(pay,1260,1280))
{
return 159;
}
else if(between(pay,1280,1300))
{
return 162;
}
else if(between(pay,1300,1320))
{
return 165;
}
但我希望它看起来像这样:
else if(between(pay,1260,1280)){return 159;}
else if(between(pay,1280,1300)){return 162;}
else if(between(pay,1300,1320)){return 165;}
我可以用bash做到吗? 如果没有,我可以使用哪种语言?
完整的代码超过30,000行,我可以手动完成,但是我知道有更好的方法。 我想说的是'sed'命令可以帮助我使用正则表达式,但是据我所知。
附注:请忽略这只是一次未优化的过程。
这可能对您有用(GNU sed):
sed '/^else/{:a;N;/^}/M!ba;s/\n\s*//g}' file
在模式空间中收集所需的行,并在遇到结束标记时删除所有换行和后面的空格,即一行的开始}
。
跟随awk也可能会帮助您。
awk -v RS="" '{
$1=$1;
gsub(/ { /,"{");
gsub(/ }/,"}");
gsub(/}/,"&\n");
gsub(/ else/,"else");
sub(/\n$/,"")
}
1
' Input_file
输出如下。
else if(between(pay,1260,1280)){return 159;}
else if(between(pay,1280,1300)){return 162;}
else if(between(pay,1300,1320)){return 165;}
编辑:现在也添加解决方案的说明。
awk -v RS="" '{ ##Making RS(record separator) as NULL here.
$1=$1; ##re-creating first field to remove new lines or space.
gsub(/ { /,"{"); ##globally substituting space { with only { here.
gsub(/ }/,"}"); ##globally substituting space } with only } here.
gsub(/}/,"&\n"); ##globally substituting } with } and new line here.
gsub(/ else/,"else");##globally substituting space else with only else here.
sub(/\n$/,"") ##substituting new line at last of line with NULL.
}
1 ##motioning 1 here as awk works on method of condition and action.
##So here I am making condition as TRUE and then not mentioning any action so be default print of current line will happen.
' Input_file
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.