Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If pattern matched delete newline character in that line

Tags:

grep

sed

awk

tr

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?

like image 873
josifoski Avatar asked Sep 20 '14 00:09

josifoski


People also ask

Does newline count as a character?

\n is a way of representing a newline character in various languages and programs but as the name suggests, a newline is only stored in a file as a single character.

What is meant by newline character?

The newline character ( \n ) is called an escape sequence, and it forces the cursor to change its position to the beginning of the next line on the screen. This results in a new line.


1 Answers

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

like image 121
Casimir et Hippolyte Avatar answered Oct 02 '22 08:10

Casimir et Hippolyte