in reply to Remove newlines and join lines

Hi, it seems indeed many solutions exist, I tried some of then and will continue to test.

Finally the "chomp" solution was easy to put in my code as I already had it and removed all unnesseray stuff. But what about if the next line contains data I wish also to have left also?

Like original file

Mary 2.12 Some text here we want too David 3.13 Even more text here
This is what we wish to have:
Mary 2.12 Some text here we want too David 3.13 Even more text here
BR Hewarn

Replies are listed 'Best First'.
Re: Re: Remove newlines and join lines
by BrowserUk (Patriarch) on Aug 28, 2002 at 19:24 UTC

    Effectively what you want to do is remove any blank lines and then join every second remaining to it's predecessor. So... (untested)

    #open INPUT and OUTPUT while (<INPUT>) { chomp; next if m/^\s+$/; my $nextline; chomp($nextline = <INPUT> ) while $nextline !~ m/^\s+/; print OUTPUT $_,' ',$nextline; } #close INPUT and OUTPUT

    What's this about a "crooked mitre"? I'm good at woodwork!
Re: Re: Remove newlines and join lines
by Django (Pilgrim) on Aug 28, 2002 at 16:00 UTC
    Replacing the regex in my first answer with that one should work:
    s/\s*(\w+)[\n\s]+([^\n]+)[\n\s]+/$1 $2\n/g;