Reputation: 1343
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:
begin(\n)*
and Replace: begin\n
(\n)*([[:space:]])*end;
and Replace: \nend;
My questions:
sed
(in-place)?end;
. How could this problem be fixed?Upvotes: 1
Views: 113
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
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
:
length
), print it and set b
to 0
(false)begin
(ignoring blank chars [\s
]), set b
to 1
(true)^$
), and b
is true (we're 1 line after begin
), skip this line (next
)begin
, print (because we don't want to eliminate all blank lines)Upvotes: 1