Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

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

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


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

Then something like this should work:

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


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

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

with sed i would do something like:

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .