tomdyson has asked for the wisdom of the Perl Monks concerning the following question:

The answer here has helped me remove single - but not multiple - line breaks. I need to extend this so that it applies only to lines exceeding a specified length, so that:
This is a longish line whose linebreak should be removed so that this line runs together with it. The preceding double linebreak should be conserved. This little line and this one should stay separated.
becomes
This is a longish line whose linebreak should be removed so that this +line runs together with it. The preceding and following double linebreaks should be conserved. This little line and this one should stay separated.
For example, I'd like to reflow the bodies of email messages while maintaining the formatting of short signatures.

Replies are listed 'Best First'.
Re: Single newline removal when line exceeds x characters
by Roger (Parson) on Oct 20, 2003 at 07:38 UTC
    Here's my solution to your little problem -

    use strict; my $prev_length = 0; my $threshold = 40; my $first_line = 1; while (<DATA>) { chomp; # s/^\s+//g; # s/\s+$//g; my $curr_length = length($_); if ($prev_length > $threshold && $curr_length > 0) { print " "; } elsif ($first_line) { $first_line = 0; } else { print "\n"; } print "$_"; $prev_length = length($_); } print "\n"; __DATA__ This is a longish line whose linebreak should be removed so that this line runs together with it. The preceding double linebreak should be conserved. This little line and this one should stay separated.
    And the output is as expected -
    This is a longish line whose linebreak should be removed so that this +line runs together with it. The preceding double linebreak should be conserved. This little line and this one should stay separated.

      Here's one with a regex and a lookahead assertion.

      local $/; $_ = <DATA>; my $wrap_min = 20; s/^(.{$wrap_min,})\n(?=\S)/$1 /gm; print; __DATA__ This is a longish line whose linebreak should be removed so that this line runs together with it. The preceding double linebreak should be conserved. This little line and this one should stay separated.

      Added space after $1 thanks Roger

      cheers

      tachyon

      s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

        Nice solution :-D. But you might want to add a space after $1. Otherwise the output becomes "This is a longish line whose linebreak should be removedso that this line runs together with it." where there is no space between the removed and so.

Re: Single newline removal when line exceeds x characters
by jmcnamara (Monsignor) on Oct 20, 2003 at 08:05 UTC

    Here is a one-liner that reads a file in paragraph mode and then removes newlines if they terminate a line longer than or equal to some limit (in this case 40):
    perl -00 -lpe 's/(.{40})\n/$1 /g' file

    --
    John.

Re: Single newline removal when line exceeds x characters
by Anonymous Monk on Oct 20, 2003 at 10:53 UTC
    Thanks, everyone, for your helpful and educational solutions.