Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question
add comment

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.

share|improve this answer
add comment

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
share|improve this answer
add comment

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

share|improve this answer
add comment

with sed i would do something like:

sed 's/[ \t]*$//'
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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