首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在空格后重新开始诗行编号的任意4行

在空格后重新开始诗行编号的任意4行
EN

Unix & Linux用户
提问于 2022-02-04 08:22:11
回答 3查看 61关注 0票数 1

这与这里给出的非常有趣的答案有关:任何文字功用或黑码诗行省略某些数字?

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

这做了自动编号诗行的工作。

但是,如果有几首诗用空白行隔开,就这样:

代码语言:javascript
复制
    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.

我不知道是否可以在空白后重新启动任意四行的行号,以便有:

代码语言:javascript
复制
    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
 4  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
 8  Things unattempted yet in Prose or Rhime.
EN

回答 3

Unix & Linux用户

发布于 2022-02-04 14:08:12

代码语言:javascript
复制
$ awk '
    !NF { c=0 }
    NF {
        sub(/^ +/,"")
        $0 = sprintf("%2s  %s", (++c%4 ? "" : c), $0)
    }
1' file
    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
 4  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
 8  Things unattempted yet in Prose or Rhime.

或者如果你喜欢:

代码语言:javascript
复制
$ awk -v RS= -F'\n' '
    NR>1 { print ORS }
    {
        for ( i=1; i<=NF; i++) {
            sub(/^ +/,"",$i)
            printf "%2s  %s\n", (i%4 ? "" : i), $i
        }
    }
' file
    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
 4  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
 8  Things unattempted yet in Prose or Rhime.
票数 1
EN

Unix & Linux用户

发布于 2022-02-08 18:16:05

使用Raku (以前称为Perl_6)

代码语言:javascript
复制
raku -e 'my @para = .split( /\n**2..*/, :skip-empty) given slurp; 
  @para = @para>>.lines>>.trim; for @para {for $_.kv -> $k,$v { 
  put ($k+1) %% 4 ?? sprintf("%6s  ", $k+1) ~ $v !! sprintf("%6s  ", "") ~ $v }; 
  "\n".put;}'  file

样本输入:

Paradise Lost,约翰·弥尔顿著(摘录):

代码语言:javascript
复制
    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.

样本输出:

Paradise Lost,约翰·弥尔顿著(摘录):

代码语言:javascript
复制
    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
 4  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
 8  Things unattempted yet in Prose or Rhime.

上面是用Raku编码的答案,Raku是Perl程序设计语言家族的成员。简单地说,这首诗是slurp-ed in和split,在任何连续的\n换行符出现时都是这样,将数据存储在@para数组中(这实际上是将诗分解为‘段落’)。在第二个语句中,使用>>超操作符来修改LHS上的每个元素,并在RHS上使用该函数。因此,@para>>.lines>>.trim链被读到,“‘@para’的每个元素都被转换为线条(即chomped),而@para的每一行都被转换为修边行”。

在下一个语句( for循环)中,对@para数组进行迭代。立即将每个(段落)元素转换为kv一个键值对,并使用嵌套的for循环进行迭代,该循环实现了Raku的三元算子($k+1) %% 4 if ?? True的每次出现都将$k+1放置在$v节的左侧,否则如果!! False - ""空字符串将被放置。最后,@para再次被\n换行符分隔。

https://raku.org

票数 1
EN

Unix & Linux用户

发布于 2022-02-04 08:50:06

如果您的目标是只有在至少有两个空行时才重置计数器,那么:

代码语言:javascript
复制
awk '{ if(/^$/&& pre==$0)c=0; else c++;
       pre=$0;
       printf "%6s %s\n", (c&&c%4==0?c:"") , $0
}' infile
票数 0
EN
页面原文内容由Unix & Linux提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://unix.stackexchange.com/questions/689235

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档