Search code examples
bashawksedline

How to merge Multiple lines into one line only if the previous line ends with backslash?


I have the following file:

Hi
How \
are \ 
you\
?

Bye

I want to get this output:

Hi
How are you?

Bye

How can I do that?


Solution

  • Assuming that \ is at the end of each line with no spaces after:

    sed -z 's/\\\n//g' file
    

    Consume the file with no line endings (-z) and then replace and \ and an new line with nothing.