alcala
alcala

Reputation: 1153

Remove extra empty lines between curly brackets

I would like to have a perl expression to run from command line to remove the extra line between the curly brackets in this case:

  // some code
  }
       <-- empty line to remove
}
// more code

Upvotes: 1

Views: 1157

Answers (2)

ysth
ysth

Reputation: 98388

If you only want it to do it when the second curly brace is at the beginning of the line:

perl -0777 -pi -we's/}\n\n}/}\n}/g' filename

If even if it is indented:

perl -0777 -pi -we's/}\n(\n[^\S\n]*(?=}))/}$1/g' filename

If there might be extra whitespace on the "empty" line or just after the first curly brace:

perl -0777 -pi -we's/(}[^\S\n]*\n)[^\S\n]*\n([^\S\n]*(?=}))/$1$2/g' filename

Upvotes: 3

Aran-Fey
Aran-Fey

Reputation: 43136

You can use regex to replace (?<=})\s*\n(?:\s*\n)+(\s*}) with \n$1. Unfortunately I don't know perl, so I don't mind if someone steals this pattern to write a complete answer.

Upvotes: 2

Related Questions