How to replace newline with space except when it's the only character in a row

1

I have some text like:

from her patient
scrutiny
and judicious criticism.

J.K.

WASHINGTON, D.C.

April, 1919.

And want to leave it like:

from her patient scrutiny and judicious criticism.

J.K.

WASHINGTON, D.C.

April, 1919.

How can I do it? I know you can replace newlines easily using the find & replace tool with /r/n but I don't know how to do this.

Some extra step: I also want to delete one newline if the previous line was empty and just had a newline character. Is that possible?

I don't know much about programming but I'm okay if I need to use bash or any Terminal tool to do this. However I hope I can use 4 cpu cores because the text file is really long.

Maccer

Posted 2019-04-29T15:40:06.677

Reputation: 111

One file or multiple? If it is one file, you cannot really parallel-process this. Please show what line ending characters your file uses. Is it \r\n\r\n for the double whitespace? Can the file be converted to Unix line endings? – slhck – 2019-04-29T16:09:39.047

It's one file sadly. But it's alright, I'll just leave it running then. The ending characters are always \r\n and also for the double whitespace (\r\n\r\n). I would prefer to have Windows line endings but it's alright anyway, I think it's going to be easy to change \r to \r\n. – Maccer – 2019-04-29T17:31:46.820

Answers

2

  • Ctrl+H
  • Find what: (?<=\S)\R(?=\S)
  • Replace with: # a space
  • check Wrap around
  • check Regular expression
  • Replace all

Explanation:

(?<=\S)     # positive lookbehind, make sure we have a NON space character before
\R          # any kind of line break
(?=\S)      # positive lookahead, make sure we have a NON space character after

Result for given example:

from her patient scrutiny and judicious criticism.

J.K.

WASHINGTON, D.C.

April, 1919.

Screen capture:

enter image description here

Toto

Posted 2019-04-29T15:40:06.677

Reputation: 7 722