If pattern matched delete newline character in that line

josifoski picture josifoski · Sep 20, 2014 · Viewed 11.3k times · Source

Let say pattern is string "Love"

input

This is some text
Love this or that
He is running like a rabbit

output

This is some text
Love this or thatHe is running like a rabbit

I've noticed that sed is very unpleasant for deleting newline characters, any idea?

Answer

Casimir et Hippolyte picture Casimir et Hippolyte · Sep 20, 2014

You can use this:

sed '/^Love/{N;s/\n//;}' love.txt

details:

/^Love/ identifies the line to treat, if you like you can use /[Ll]ove/ instead

N adds the next line to the pattern space. After this command the pattern space contains Love this or that\nHe is running like a rabbit

s/\n// replaces the newline character