[英]How can I make multiple lines into one line using bash?
So I have code that looks like this: 所以我有这样的代码:
else if(between(pay,1260,1280))
{
return 159;
}
else if(between(pay,1280,1300))
{
return 162;
}
else if(between(pay,1300,1320))
{
return 165;
}
But I want it to look like this: 但我希望它看起来像这样:
else if(between(pay,1260,1280)){return 159;}
else if(between(pay,1280,1300)){return 162;}
else if(between(pay,1300,1320)){return 165;}
Can I do this in bash? 我可以用bash做到吗? If not, which language can I use?
如果没有,我可以使用哪种语言?
The full code is over 30,000 lines and I could manually do it, but I know there's a better way. 完整的代码超过30,000行,我可以手动完成,但是我知道有更好的方法。 I want to say the 'sed' command can help me with a mixture of regex, but that's as far as my knowledge can take me.
我想说的是'sed'命令可以帮助我使用正则表达式,但是据我所知。
PS Please overlook how un-optimized it is just this once. 附注:请忽略这只是一次未优化的过程。
This might work for you (GNU sed): 这可能对您有用(GNU sed):
sed '/^else/{:a;N;/^}/M!ba;s/\n\s*//g}' file
Gather up the required lines in the pattern space and remove all newlines and following spaces on encountering the end marker ie a line beginning }
. 在模式空间中收集所需的行,并在遇到结束标记时删除所有换行和后面的空格,即一行的开始
}
。
Following awk may also help you in same. 跟随awk也可能会帮助您。
awk -v RS="" '{
$1=$1;
gsub(/ { /,"{");
gsub(/ }/,"}");
gsub(/}/,"&\n");
gsub(/ else/,"else");
sub(/\n$/,"")
}
1
' Input_file
Output will be as follows. 输出如下。
else if(between(pay,1260,1280)){return 159;}
else if(between(pay,1280,1300)){return 162;}
else if(between(pay,1300,1320)){return 165;}
EDIT: Adding explanation for solution too now. 编辑:现在也添加解决方案的说明。
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.