Search code examples
sedlabelconcatenation

How to concatenate two lines using label in sed


I have a question if there is some way to detect the end of the line using sed. Bcs I need to concatenate two-line only if the end of the line end with a minus sign - otherwise not.

sed -e :a -e '/,$/N; s/-\n*/new_line/; ta' test.txt (that is only what i have and i need to substite new_line for actualy new line)

if file is look something like that

Here is a random sente-
nc and if random sentec ended with minus it is better to concatenate.

RESULT

Here is a random sentece and if random sentec ended with minus it is better to concatenate.

Solution

  • Here is how you will do that sed in any version:

    cat file
    
    foo bar_-
    Here is a random sente-
    nce and if random sentec ended with minus it is better to concatenate.
    
    # and use sed as
    sed -e :a -e '/-$/{N;s/-\n//g;ta' -e '}' file
    
    foo bar_Here is a random sentence and if random sentec ended with minus it is better to concatenate.