我第一次使用 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
因为这只是新手拍摄。