Bash Script To Change Curly Brace Style

人走茶凉 提交于 2019-12-12 14:40:06

问题


I have a CSS file and a PHP file that I received from an overseas outsource partner. He prefers curly braces on a new line, while I am rather Old School and prefer the curly brace on the same line as the declaration. How can I use Bash and/or sed or other command-line tools to revert curly braces from this new style and into this older style?

EDIT: Someone wanted to see an example. Okay, here goes:

NEW SCHOOL STYLE I DO NOT LIKE

body 
{
padding:4px;
margin:3px;
}

OLD SCHOOL I PREFER

body {
padding:4px;
margin:3px;
}

NEW SCHOOL STYLE I DO NOT LIKE

function foo() 
{
// some code here
}

OLD SCHOOL STYLE I PREFER

function foo() {
// some code here
}

回答1:


 sed 'N;/\n{/s// {/;P;D' file.css

Input

$ cat file.css
body
{
background-color:#d0e4fe;
}
h1
{
color:orange;
text-align:center;
}
p
{
font-family:"Times New Roman";
font-size:20px;
}

Output

$ sed 'N;/\n{/s// {/;P;D' file.css
body {
background-color:#d0e4fe;
}
h1 {
color:orange;
text-align:center;
}
p {
font-family:"Times New Roman";
font-size:20px;
}



回答2:


A start might be:

sed 'N;/\n{/s/\n/ /;P;D' inputfile

If a line begins with a curly brace, it will be appended to the previous line (with an added space).



来源:https://stackoverflow.com/questions/4521618/bash-script-to-change-curly-brace-style

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!