gpt4 book ai didi

macos - 使用 sed 在文本范围下方插入行

转载 作者:行者123 更新时间:2023-12-01 07:28:33 25 4
gpt4 key购买 nike

我有一段文本,其中一些部分用四空格缩进清楚地描绘出来:

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.

我希望每个缩进 block 的前面紧跟着 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在第一个引用 block 上。我认为这里发生的是脚本中的第二个命令:

/^    / {
N
/\n$/ {
s/\n$/&END QUOTE/
G
}
}

只有在当前行是引用 block 的最后一行时才能正确找到 block 末尾的边界。但有时,它会相差一个,边界会被分成两个独立的N。命令,因此无法识别。关于使用 sed 执行此操作的正确方法的任何指示是吗?

最佳答案

使用 sed

当寻找报价的结尾时,原始脚本成对地读取。因此,只有当引用包含奇数行时才会发现引用的结尾。解决方案是立即读取整个引用,然后将 END QUOTE 添加到它的末尾:

#!/usr/bin/sed -Ef
/^$/ {
N
/\n / {
P
s/^\n//
i\
START QUOTE
}
}

/^ / {
:a;N;/\n$/!ba
s/$/END QUOTE\n/
}

这里的关键变化是 :a;N;/\n$/!ba,它读入行直到找到一个空行。

[以上是在GNU sed下测试的。 BSD (OSX) sed 通常略有不同。]

使用 awk

sed 可以做任何事情,但是逻辑复杂的事情通常用 awk 更容易做。对于您的问题,请尝试:

awk '/^    / && q{print;next} q{print "END QUOTE"; q=0} /^    /{print "START QUOTE"; q=1} 1' file

根据您的输入,例如:

$ awk '/^    / && q{print;next} q{print "END QUOTE"; q=0} /^    /{print "START QUOTE"; q=1} 1' file
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.
END QUOTE

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.

工作原理

此脚本使用单个变量 q,当我们在引号中时为 1,否则为 0。

  • /^/&& q{print;next}

    如果 q 为真并且该行以 4 个空格开始,则打印该行,跳过其余命令并跳转到 行。

  • q{print "END QUOTE"; q=0}

    如果我们在 q 为真时到达此处,则此行不以 4 个空格开头。这意味着报价刚刚结束,我们打印 END QUOTE 并将 q 重置为 false (0)。

  • /^/{print "START QUOTE"; q=1}

    如果我们到达这里的一行以 4 个空格开头,那么引号才刚刚开始。我们打印 START QUOTE 并将 q 设置为 true (1)。

  • 1

    这是 awk 用于打印该行的神秘速记。

关于macos - 使用 sed 在文本范围下方插入行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37755054/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com