Reputation: 43
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
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
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