AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / unix / 问题 / 680747
Accepted
Dingo
Dingo
Asked: 2021-12-09 16:53:28 +0800 CST2021-12-09 16:53:28 +0800 CST 2021-12-09 16:53:28 +0800 CST

任何文本实用程序或破解省略某些数字的诗行编号?

  • 772

我的目标是尝试查看 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,
utilities text
  • 2 2 个回答
  • 291 Views

2 个回答

  • Voted
  1. Best Answer
    cas
    2021-12-09T18:15:48+08:002021-12-09T18:15:48+08:00

    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第二行之后。

    • 1
  2. jubilatious1
    2022-02-09T11:23:52+08:002022-02-09T11:23:52+08:00

    使用 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 编码的答案,Raku 是 Perl 编程语言家族的成员。简而言之,这首诗是使用 Raku 的lines例程(autochomps)阅读的,每一行都是trim-med。数据存储在@lines1数组中。

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

    请参阅this SO answer了解如何重新开始行号,例如当一首诗被分成段落/节时。

    https://raku.org

    • 1

相关问题

  • 是否有任何“基础”Debian 元包?

  • 如何获取以太网中数据收发状态的信息?

  • 使用所有组合生成词表

  • 将字符串插入文本文件中的列表变量 [使用 sed]

  • 在 Taskwarrior 中过滤为未来安排的任务

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    模块 i915 可能缺少固件 /lib/firmware/i915/*

    • 3 个回答
  • Marko Smith

    无法获取 jessie backports 存储库

    • 4 个回答
  • Marko Smith

    如何将 GPG 私钥和公钥导出到文件

    • 4 个回答
  • Marko Smith

    我们如何运行存储在变量中的命令?

    • 5 个回答
  • Marko Smith

    如何配置 systemd-resolved 和 systemd-networkd 以使用本地 DNS 服务器来解析本地域和远程 DNS 服务器来解析远程域?

    • 3 个回答
  • Marko Smith

    dist-upgrade 后 Kali Linux 中的 apt-get update 错误 [重复]

    • 2 个回答
  • Marko Smith

    如何从 systemctl 服务日志中查看最新的 x 行

    • 5 个回答
  • Marko Smith

    Nano - 跳转到文件末尾

    • 8 个回答
  • Marko Smith

    grub 错误:你需要先加载内核

    • 4 个回答
  • Marko Smith

    如何下载软件包而不是使用 apt-get 命令安装它?

    • 7 个回答
  • Martin Hope
    user12345 无法获取 jessie backports 存储库 2019-03-27 04:39:28 +0800 CST
  • Martin Hope
    Carl 为什么大多数 systemd 示例都包含 WantedBy=multi-user.target? 2019-03-15 11:49:25 +0800 CST
  • Martin Hope
    rocky 如何将 GPG 私钥和公钥导出到文件 2018-11-16 05:36:15 +0800 CST
  • Martin Hope
    Evan Carroll systemctl 状态显示:“状态:降级” 2018-06-03 18:48:17 +0800 CST
  • Martin Hope
    Tim 我们如何运行存储在变量中的命令? 2018-05-21 04:46:29 +0800 CST
  • Martin Hope
    Ankur S 为什么 /dev/null 是一个文件?为什么它的功能不作为一个简单的程序来实现? 2018-04-17 07:28:04 +0800 CST
  • Martin Hope
    user3191334 如何从 systemctl 服务日志中查看最新的 x 行 2018-02-07 00:14:16 +0800 CST
  • Martin Hope
    Marko Pacak Nano - 跳转到文件末尾 2018-02-01 01:53:03 +0800 CST
  • Martin Hope
    Kidburla 为什么真假这么大? 2018-01-26 12:14:47 +0800 CST
  • Martin Hope
    Christos Baziotis 在一个巨大的(70GB)、一行、文本文件中替换字符串 2017-12-30 06:58:33 +0800 CST

热门标签

linux bash debian shell-script text-processing ubuntu centos shell awk ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve