2

我正在尝试在文档中加入句子,但其中一些句子已经分开,中间有一个空行。例如:

狗追了一个球

那是它的主人扔的。

球飞得很远。

到:

这只狗追着它主人扔的一个球。

球飞得很远。

我在想我可以搜索一个空行,然后在下一行的开头搜索一个小写字符。它复制该行,删除它及其上方的空行,然后将复制的句子附加到另一个损坏的句子(抱歉造成混淆)。

我是 sed 的新手,并使用以下命令进行了尝试:

sed "/$/{:a;N;s/\n\(^[a-z]* .*\)/ \1/;ba}"

但只做一次,只删除空行,而不是将断句的第二半附加到第一部分。

请帮忙。

4

3 回答 3

1

这应该可以解决问题:

sed ':a;$!{N;N};s/\n\n\([a-z]\)/ \1/;ta;P;D' sentences
于 2010-01-15T23:01:00.003 回答
0

我第一次使用 sed 执行如此复杂的替换。我花了大约 2 个小时才想出一些东西:D

我使用了 GNU sed,因为我无法在我的 mac 上单行进行分支工作。

这是我用于测试的输入内容:

The dog chased after a ball

that was thrown by its owner.

The ball

travelled quite far.
I took me a while to fix this file.
And now it's

working :)

然后这是sed我想出的命令行:

$ sed -n '/^$/!bstore;/^$/N;s/\n\([a-z]\)/ \1/;tmerge;h;d;:store;H;b;:merge;H;g;s/\n \([a-z]\)/ \1/;p;s/.*//g;h;d' sentences.txt

这是输出:

$ sed -n '/^$/!bstore;/^$/N;s/\n\([a-z]\)/ \1/;tmerge;h;d;:store;H;b;:merge;H;g;s/\n \([a-z]\)/ \1/;p;s/.*//g;h;d' sentences.txt

The dog chased after a ball that was thrown by its owner.

The ball travelled quite far.

I took me a while to fix this file.
And now it's working :)

您会注意到在开头插入了一个空行,但我认为可以忍受。请各位,如果您正在掌握,请对此发表评论,sed因为这只是新手拍摄。

于 2010-01-15T22:30:37.583 回答
0

如果你有 Python,你可以试试这个片段

import string
f=0
data=open("file").readlines()
alen=len(data)
for n,line in enumerate(data):
    if line[0] in string.uppercase:
        found_upper=n
        f=1
    if f and line[0] in string.lowercase:
        data[found_upper] = data[found_upper].strip() + " " + line
        data[n]=""
    if n+1==alen:
        if line[0] in string.lowercase:
            data[found_upper] = data[found_upper].strip() + " " + line
            data[n]=""
        else : data[n]=line

输出(添加更多文件格式的场景)

$  cat file    
the start
THE START
The dog chased after a ball
that was thrown by its owner.

My ball travelled quite far




and it smashed the windows
but it didn't cause much damage


THE END
THE FINAL DESTINATION
final
FINAL DESTINATION LAST EPISODE
the final final

$ ./python.py
the start
THE START
The dog chased after a ball that was thrown by its owner.

My ball travelled quite far and it smashed the windows but it didn't cause much damage






THE END
THE FINAL DESTINATION final
FINAL DESTINATION LAST EPISODE the final final the final final
于 2010-01-16T06:50:53.350 回答