且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

bash脚本更改大括号风格

更新时间:2022-12-09 09:31:23

 的sed'N; / \\ N {/秒// {/; P; D'file.css

输入

  $猫file.css
身体
{
背景颜色:#d0e4fe;
}
H1
{
颜色:橙色;
文本对齐:中心;
}
p
{
FONT-FAMILY:宋体;
字体大小:20像素;
}

输出

  $ SED'N; / \\ N {/秒// {/; P; D'file.css
身体 {
背景颜色:#d0e4fe;
}
H1 {
颜色:橙色;
文本对齐:中心;
}
p {
FONT-FAMILY:宋体;
字体大小:20像素;
}

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
}

 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;
}