[GLLUG] Replacing newlines in text file

Andy Smith andy at bitfolk.com
Sun Aug 30 09:55:17 UTC 2015


Hi Dylan,

On Sun, Aug 30, 2015 at 10:37:54AM +0100, Dylan wrote:
> I have a text file with 'hard' newlines at the end of each 'line'
> (often mid-sentence,) and multiple newlines at the end of paragraphs.
> I need a simple incantation to change all occurrences of a *single*
> newline to a space, and leave the multiple newlines in place.

You probably want something based on sed, e.g. from
http://stackoverflow.com/questions/1251999/how-can-i-replace-a-newline-n-using-sed

    sed ':a;N;$!ba;s/\n/ /' source.txt > dest.txt

If you just wanted to replace every single newline with a space than
tr would be much simpler:

    tr '\n' ' '

But that doesn't appear to be what you asked for (you asked for only
the first newline).

The invocation of sed is more complicated because sed is
line-oriented.

Cheers,
Andy

-- 
http://bitfolk.com/ -- No-nonsense VPS hosting



More information about the GLLUG mailing list