【发布时间】:2016-06-13 06:18:33
【问题描述】:
我有一段文本,其中的某些部分用四空格缩进清楚地划定:
PERCHANCE he for whom this bell tolls may be so ill, as that he knows not it
tolls for him; and perchance I may think myself so much better than I am, as
that they who are about me, and see my state, may have caused it to toll for me,
and I know not that.
The church is Catholic, universal, so are all her actions; all that she does
belongs to all. When she baptizes a child, that action concerns me; for that
child is thereby connected to that body which is my head too, and ingrafted into
that body whereof I am a member.
And when she buries a man, that action concerns me: all mankind is of one
author, and is one volume; when one man dies, one chapter is not torn out of the
book, but translated into a better language; and every chapter must be so
translated; God employs several translators; some pieces are translated by age,
some by sickness, some by war, some by justice; but God's hand is in every
translation, and his hand shall bind up all our scattered leaves again for that
library where every book shall lie open to one another.
As therefore the bell that rings to a sermon calls not upon the preacher only,
but upon the congregation to come, so this bell calls us all; but how much more
me, who am brought so near the door by this sickness.
There was a contention as far as a suit (in which both piety and dignity,
religion and estimation, were mingled), which of the religious orders should
ring to prayers first in the morning; and it was determined, that they should
ring first that rose earliest.
我希望每个缩进块的前面紧跟START QUOTE,紧跟在END QUOTE 后面。我一直在玩 sed 十五分钟,但仍然不能完全正确。到目前为止,这是我的最大努力:
#!/usr/bin/sed -Ef
/^$/ {
N
/\n / {
P
s/^\n//
i\
START QUOTE
}
}
/^ / {
N
/\n$/ {
s/\n$/&END QUOTE/
G
}
}
运行./parse.sed <script.txt,我得到以下输出:
PERCHANCE he for whom this bell tolls may be so ill, as that he knows not it
tolls for him; and perchance I may think myself so much better than I am, as
that they who are about me, and see my state, may have caused it to toll for me,
and I know not that.
START QUOTE
The church is Catholic, universal, so are all her actions; all that she does
belongs to all. When she baptizes a child, that action concerns me; for that
child is thereby connected to that body which is my head too, and ingrafted into
that body whereof I am a member.
And when she buries a man, that action concerns me: all mankind is of one
author, and is one volume; when one man dies, one chapter is not torn out of the
book, but translated into a better language; and every chapter must be so
translated; God employs several translators; some pieces are translated by age,
some by sickness, some by war, some by justice; but God's hand is in every
translation, and his hand shall bind up all our scattered leaves again for that
library where every book shall lie open to one another.
START QUOTE
As therefore the bell that rings to a sermon calls not upon the preacher only,
but upon the congregation to come, so this bell calls us all; but how much more
me, who am brought so near the door by this sickness.
END QUOTE
There was a contention as far as a suit (in which both piety and dignity,
religion and estimation, were mingled), which of the religious orders should
ring to prayers first in the morning; and it was determined, that they should
ring first that rose earliest.
请注意第一个引用块上缺少的END QUOTE。我认为这里发生的事情是脚本中的第二个命令:
/^ / {
N
/\n$/ {
s/\n$/&END QUOTE/
G
}
}
只有在当前行是引用块的最后一行时才能正确找到块末尾的边界。但有时,它会偏离 1,并且边界会被两个单独的 N 命令摄取,因此无法识别。关于使用sed 的正确方法是什么?
【问题讨论】: