user3657270
user3657270

Reputation: 43

Changing one textline into multiple textlines

I am trying to split this line into multiple lines, each containing a separate sentence:

This is a first sentence. This is a second sentence. Look at this: a third sentence! Where does this ends? I have no Idea, who knows…

We have to use the shortest command that is possible.

I tried this :

sed 's/[.?!]/&\n/g'

But it adds one single space after every new sentence :

This is a first sentence.
 This is a second sentence.
 Look at this: a third sentence!
 Where does this ends? I have no Idea, who knows…    

Keep in mind that we have to keep the line as short as possible

Upvotes: 0

Views: 59

Answers (3)

Scrutinizer
Scrutinizer

Reputation: 9936

Try with GNU sed:

sed -r 's/([.?!]+) */\1\n/g' file

With regular sed:

sed 's/\([.?!]\{1,\}\) */\1\
/g' file

These test for one or more occurrences of sentence terminators followed by 0 or more spaces..

Upvotes: 2

Jotne
Jotne

Reputation: 41460

You can use awk

awk '{gsub(/\. /,"."RS);gsub(/\? /,"?"RS);gsub(/\! /,"!"RS)}1' file
THis is a first sentence.
This is a second sentence.
Look at this: a third sentence!
Where does this ends?
I have no Idea, who knows.

Or this: (it adds a space at end of the line)

awk '{gsub(/[.!?] /,"&"RS)}1' file
THis is a first sentence.
This is a second sentence.
Look at this: a third sentence!
Where does this ends?
I have no Idea, who knows.

Upvotes: 0

Avinash Raj
Avinash Raj

Reputation: 174786

Try this,

sed 's/[.?!]/&\n/g' file | sed 's/^ //g'

Upvotes: 0

Related Questions