Marcello90
Marcello90

Reputation: 1343

Remove unnecessary blank lines

Let's imagine that the following code example is valid:

begin
   statement1;

   begin


      statement2;

      statement3;
      statement4;

      statement5;

   end;

   statement6;

end;

I would like to remove all unnecessary blank lines in this code example:

begin
   statement1;

   begin
      statement2;

      statement3;
      statement4;

      statement5;
   end;

   statement6;
end;

So basically if a line ends with the keyword begin then all blank lines until the next line that contains a statement should be removed and if a line ends with the keyword end; then all blank lines until the previous line that contains a statement should be removed.

Using Sublime Text I created two regular expressions:

  1. Find: begin(\n)* and Replace: begin\n
  2. Find: (\n)*([[:space:]])*end; and Replace: \nend;

My questions:

  1. How can I convert both regular expression so that they can be used with sed (in-place)?
  2. The second regular expression drops all existing blank spaces before the keyword end;. How could this problem be fixed?

Upvotes: 1

Views: 113

Answers (2)

Walter A
Walter A

Reputation: 19982

With GNU sed 4.2 you have the option -z:

sed -rz 's/begin\n+/begin\n/g;s/\n+([^\n]*end;)/\n\1/g' file

Work-around with older sed (when original file is without \r)

tr '\n' '\r' < file | sed -r 's/begin\r+/begin\r/g;s/\r+([^\r]*end;)/\r\1/g' | tr '\r' '\n'

Upvotes: 2

Moshe
Moshe

Reputation: 5129

Not the most elegant solution, but I'll continue trying this one :)

cat file | awk '
length { print; b=0 }
/begin/ { b=1 }
/^$/ && b { next }
!length && !b { print }
' | tac | awk '
length { print; b=0 }
/end;/ { b=1 }
/^$/ && b { next }
!length && !b { print }
' | tac 

I'm running the same awk, but I invert the file for the end; part (tac).

Explanation of the awk:

  1. If the line is not empty (length), print it and set b to 0 (false)
  2. If the line starts with the string begin (ignoring blank chars [\s]), set b to 1 (true)
  3. If the line is blank (^$), and b is true (we're 1 line after begin), skip this line (next)
  4. If the line is blank and we're not after begin, print (because we don't want to eliminate all blank lines)

Upvotes: 1

Related Questions