任何文本实用程序或黑客对省略某些数字的诗行进行编号?

任何文本实用程序或黑客对省略某些数字的诗行进行编号?

我的目标是尝试看看 Linux 中是否有常见的文本实用程序可用于对诗行进行编号,如下所示:

OF Mans First Disobedience, and the Fruit
Of that Forbidden Tree, whose mortal tast
Brought Death into the World, and all our woe,
With loss of Eden, till one greater Man
Restore us, and regain the blissful Seat,
Sing Heav'nly Muse, that on the secret top
Of Oreb, or of Sinai, didst inspire
That Shepherd, who first taught the chosen Seed,
In the Beginning how the Heav'ns and Earth
Rose out of Chaos: or if Sion Hill
Delight thee more, and Siloa's brook that flow'd
Fast by the Oracle of God; I thence
Invoke thy aid to my adventrous Song,
That with no middle flight intends to soar
Above th' Aonian Mount, while it pursues
Things unattempted yet in Prose or Rhime.
And chiefly Thou, O Spirit, that dost prefer
Before all Temples th' upright heart and pure,
Instruct me, for Thou know'st; Thou from the first
Wast present, and with mighty wings outspread
Dove-like satst brooding on the vast Abyss
And mad'st it pregnant: What in me is dark
Illumin, what is low raise and support;
That to the highth of this great Argument
I may assert Eternal Providence,
And justifie the wayes of God to men.

得到这个:

        OF Mans First Disobedience, and the Fruit
        Of that Forbidden Tree, whose mortal tast
        Brought Death into the World, and all our woe,
     4  With loss of Eden, till one greater Man
        Restore us, and regain the blissful Seat,
        Sing Heav'nly Muse, that on the secret top
        Of Oreb, or of Sinai, didst inspire
     8  That Shepherd, who first taught the chosen Seed,
        In the Beginning how the Heav'ns and Earth
        Rose out of Chaos: or if Sion Hill
        Delight thee more, and Siloa's brook that flow'd
    12  Fast by the Oracle of God; I thence
        Invoke thy aid to my adventrous Song,
        That with no middle flight intends to soar
        Above th' Aonian Mount, while it pursues
    16  Things unattempted yet in Prose or Rhime.
        And chiefly Thou, O Spirit, that dost prefer
        Before all Temples th' upright heart and pure,
        Instruct me, for Thou know'st; Thou from the first
    20  Wast present, and with mighty wings outspread
        Dove-like satst brooding on the vast Abyss
        And mad'st it pregnant: What in me is dark
        Illumin, what is low raise and support;
    24  That to the highth of this great Argument
        I may assert Eternal Providence,
        And justifie the wayes of God to men.

awk 也可以用这种方式缩进行

          OF Mans First Disobedience, and the Fruit
        Of that Forbidden Tree, whose mortal tast
          Brought Death into the World, and all our woe,
     4  With loss of Eden, till one greater Man
          Restore us, and regain the blissful Seat,
        Sing Heav'nly Muse, that on the secret top
          Of Oreb, or of Sinai, didst inspire
     8  That Shepherd, who first taught the chosen Seed,

还有这样的吗?

        OF Mans First Disobedience, and the Fruit
          Of that Forbidden Tree, whose mortal tast
        Brought Death into the World, and all our woe,
     4    With loss of Eden, till one greater Man
        Restore us, and regain the blissful Seat,
          Sing Heav'nly Muse, that on the secret top
        Of Oreb, or of Sinai, didst inspire
     8    That Shepherd, who first taught the chosen Seed,

答案1

awk可能是最合适的工具。

$ awk 'FNR % 4 == 0 { printf "%6i  %s\n", FNR, $0 ; next }; {printf "%6s  %s\n", "", $0}' poem.txt 
        OF Mans First Disobedience, and the Fruit
        Of that Forbidden Tree, whose mortal tast
        Brought Death into the World, and all our woe,
     4  With loss of Eden, till one greater Man
        Restore us, and regain the blissful Seat,
        Sing Heav'nly Muse, that on the secret top
        Of Oreb, or of Sinai, didst inspire
     8  That Shepherd, who first taught the chosen Seed,
        In the Beginning how the Heav'ns and Earth
        Rose out of Chaos: or if Sion Hill
        Delight thee more, and Siloa's brook that flow'd
    12  Fast by the Oracle of God; I thence
        Invoke thy aid to my adventrous Song,
        That with no middle flight intends to soar
        Above th' Aonian Mount, while it pursues
    16  Things unattempted yet in Prose or Rhime.
        And chiefly Thou, O Spirit, that dost prefer
        Before all Temples th' upright heart and pure,
        Instruct me, for Thou know'st; Thou from the first
    20  Wast present, and with mighty wings outspread
        Dove-like satst brooding on the vast Abyss
        And mad'st it pregnant: What in me is dark
        Illumin, what is low raise and support;
    24  That to the highth of this great Argument
        I may assert Eternal Providence,
        And justifie the wayes of God to men.

每当当前输入文件 ( ) 的行号FNR可以被 4 整除时,就打印带有行号的行。否则打印时缩进相同数量的空格。


对于额外的缩进,请尝试:

awk 'FNR % 4 == 0 { printf "%6i  %s\n", FNR, $0 ; next };
     NR % 4 == 1 || NR % 4 == 3 { printf "%6s    %s\n", "", $0 ; next };
     {printf "%6s  %s\n", "", $0}' poem.txt

%6s请注意第二行后面额外的两个空格。

并且,对于第二种缩进样式:

awk 'FNR % 4 == 0 { printf "%6i    %s\n", FNR, $0 ; next };
     FNR % 4 == 2 { printf "%6s    %s\n",  "", $0 ; next };
     {printf "%6s  %s\n", "", $0}' poem.txt

这里额外的空格位于%6i第一行和%6s第二行之后。

答案2

使用 Raku(以前称为 Perl_6)

raku -e 'my @lines1 = lines>>.trim; for @lines1.kv -> $k,$v { put ($k+1) %% 4  \
         ?? sprintf("%6s  ", $k+1) ~ $v !! sprintf("%6s  ", "") ~ $v};'  file

样本输入:

失乐园,作者:约翰·弥尔顿(摘录):

OF Mans First Disobedience, and the Fruit
Of that Forbidden Tree, whose mortal tast
Brought Death into the World, and all our woe,
With loss of Eden, till one greater Man
Restore us, and regain the blissful Seat,
Sing Heav'nly Muse, that on the secret top
Of Oreb, or of Sinai, didst inspire
That Shepherd, who first taught the chosen Seed,
In the Beginning how the Heav'ns and Earth
Rose out of Chaos: or if Sion Hill
Delight thee more, and Siloa's brook that flow'd
Fast by the Oracle of God; I thence
Invoke thy aid to my adventrous Song,
That with no middle flight intends to soar
Above th' Aonian Mount, while it pursues
Things unattempted yet in Prose or Rhime.
And chiefly Thou, O Spirit, that dost prefer
Before all Temples th' upright heart and pure,
Instruct me, for Thou know'st; Thou from the first
Wast present, and with mighty wings outspread
Dove-like satst brooding on the vast Abyss
And mad'st it pregnant: What in me is dark
Illumin, what is low raise and support;
That to the highth of this great Argument
I may assert Eternal Providence,
And justifie the wayes of God to men.

样本输出:

失乐园,作者:约翰·弥尔顿(摘录):

    OF Mans First Disobedience, and the Fruit
    Of that Forbidden Tree, whose mortal tast
    Brought Death into the World, and all our woe,
 4  With loss of Eden, till one greater Man
    Restore us, and regain the blissful Seat,
    Sing Heav'nly Muse, that on the secret top
    Of Oreb, or of Sinai, didst inspire
 8  That Shepherd, who first taught the chosen Seed,
    In the Beginning how the Heav'ns and Earth
    Rose out of Chaos: or if Sion Hill
    Delight thee more, and Siloa's brook that flow'd
12  Fast by the Oracle of God; I thence
    Invoke thy aid to my adventrous Song,
    That with no middle flight intends to soar
    Above th' Aonian Mount, while it pursues
16  Things unattempted yet in Prose or Rhime.
    And chiefly Thou, O Spirit, that dost prefer
    Before all Temples th' upright heart and pure,
    Instruct me, for Thou know'st; Thou from the first
20  Wast present, and with mighty wings outspread
    Dove-like satst brooding on the vast Abyss
    And mad'st it pregnant: What in me is dark
    Illumin, what is low raise and support;
24  That to the highth of this great Argument
    I may assert Eternal Providence,
    And justifie the wayes of God to men.

上面是用 Raku(Perl 编程语言家族的成员)编写的答案。简而言之,这首诗是使用 Raku 的lines例程(自动咀嚼)来朗读的,并且每一行都是trim-med 的。数据存储在@lines1数组中。

在下一个语句(for循环)中,@lines1数组被转换为kv键值对,并进行迭代。循环块实现了 Raku 的三元运算符。每次出现($k+1) %% 4if ??True 时都会放置$k+1在该节的左侧$v,否则如果False则放置!!一个空字符串。""

这个答案了解如何重新开始行编号,例如当一首诗被分成段落/节时。

https://raku.org

相关内容