I've got a block of text with some sections that are clearly delineated by four-space indentation:
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.
I'd like each indented block to be immediately preceded with START QUOTE
and immediately followed with END QUOTE
. I've been playing around with sed for fifteen minutes but still can't get it quite right. Here's my best effort so far:
#!/usr/bin/sed -Ef
/^$/ {
N
/\n / {
P
s/^\n//
i\
START QUOTE
}
}
/^ / {
N
/\n$/ {
s/\n$/&END QUOTE/
G
}
}
Running ./parse.sed <script.txt
, I get the following output:
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.
Note the missing END QUOTE
on the first quoted block. I think what's going on here is that the second command in the script:
/^ / {
N
/\n$/ {
s/\n$/&END QUOTE/
G
}
}
only properly finds the boundary at the end of the block if the current line is the last line of the quote block. But sometimes, it's off by one, and the boundary gets ingested in two separate N
commands, and thus not recognized. Any pointers on what the right way to do this with sed
is?