Please see the image:

alt text

What regex would delete all line endings only from non-blank lines (not deleting them from blank lines? This is from a text file of over 8000 lines.

64-bit Vista.

link|improve this question

20% accept rate
feedback

4 Answers

My messy method would be to open it in word, do a find and replace on ^p^p (two end paragraphs in a row) with some character not used in the file, like "|". Then I would replace all ^p with just a space. Then I would go back and replace t he "|" with ^p.

link|improve this answer
feedback

If you're trying to convert paragraphs that have line breaks at the end of each line to continuous text within each paragraph:

Now is the time for all good\n
men to come to the aid of their\n
country\n
\n

into

Now is the time for all good men to come to the aid of their country\n
\n

Then something like this should work:

sed -n '1{x;d};H;${x;s|\([^\n]\)\n\([^\n]\)|\1 \2|gp}' file

or

sed ':a;$!N;s|^\n||;s|\n\([^\n]\+\)$| \1|;ta;p;D' file
link|improve this answer
feedback

It kind of depends on what regex package you have, whether you have lookahead or not.

I'd personally do:

-- remove trailing whitespace, this makes sure that 'blank' lines are \n\n

s/^[ \t][ \t]*$//

-- if it's a singular linefeed, substitute

s/([^\n])\n([^\n])/\1 \2/

it really depends on your regex package

link|improve this answer
feedback

with sed i would do something like:

sed 's/[ \t]*$//'
link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.