Clean and keep line breaks

Hello,

I want to keep line spaces in comments but clean more then 2 after each.

Example:

  $sentence="This is my first sentence
  This will be in a new row
  This will be too in a new row but not separated with 3line breaks just with
   
  one
   
  ";

And i want to output just this:

  $sentence="This is my first sentence
  This will be in a new row
   
  This will be too in a new row but not separated with 3line breaks just with
  one
  ";

Please Help

Thanks in Advance

First: From the way this is worded, I assume it is homework incorrectly filed in a non-homework forum.

Second: I don't see any "comments" in your input; I just see a quoted string.

Third: I'm not sure what you mean by "line spaces". I assume you're talking about empty lines (lines where the only character on the line is the <newline> terminating the line) or blank lines (lines where the only characters on the line are <space> and <tab> characters followed by the <newline> terminating the line). But, while you talk about removing "line spaces", your example adds an empty or blank line in one place and removes one in two other places.

This has probably been sitting here unanswered for almost a full day because we can't figure out what you want.