Linux – Bash script that changes the curly brace style

Bash script that changes the curly brace style… here is a solution to the problem.

Bash script that changes the curly brace style

I

have a CSS file and a PHP file that I received from an overseas outsourcing partner. He prefers braces on new lines, while I’m older school and prefer braces on the same line as the statement. How do I revert curly braces from this new style to this old style using Bash and/or sed or other command-line tools?

EDIT: Someone wants to see an example. Okay, here we go:

I don’t like the new school style

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

I prefer the old school

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

I don’t like the new school style

function foo() 
{
 some code here
}

I like the old school style

function foo() {
 some code here
}

Solution

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

Enter

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

Related Problems and Solutions