我试图将这一行分成多行,每行包含一个单独的句子:
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…
我们必须使用尽可能短的命令。
我试过了:
sed 's/[.?!]/&\n/g'
但它会在每个新句子后添加一个空格:
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…
请记住,我们必须尽量缩短线路
答案 0 :(得分:2)
尝试使用GNU sed:
sed -r 's/([.?!]+) */\1\n/g' file
使用常规sed:
sed 's/\([.?!]\{1,\}\) */\1\
/g' file
这些测试用于一个或多个句子终止符后跟0个或多个空格。
答案 1 :(得分:0)
试试这个,
sed 's/[.?!]/&\n/g' file | sed 's/^ //g'
答案 2 :(得分:0)
您可以使用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.
或者这个:(它在行尾添加一个空格)
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.