我的目标是尝试查看 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,
awk
可能是最合适的工具。每当当前输入文件 (
FNR
) 的行号可以被 4 整除时,打印带有行号的行。否则以相同数量的空格缩进打印。对于额外的缩进,尝试:
%6s
注意第二行后面额外的两个空格。并且,对于第二个缩进样式:
这里多余的空格
%6i
在第一行和%6s
第二行之后。使用 Raku(以前称为 Perl_6)
样品输入:
失乐园,约翰·米尔顿(摘录):
样品输出:
失乐园,约翰·米尔顿(摘录):
上面是用 Raku 编码的答案,Raku 是 Perl 编程语言家族的成员。简而言之,这首诗是使用 Raku 的
lines
例程(autochomps)阅读的,每一行都是trim
-med。数据存储在@lines1
数组中。在下一条语句(
for
循环)中,@lines1
数组被转换为kv
键值对,并进行迭代。循环块实现了 Raku 的三元运算符。每次出现的($k+1) %% 4
if??
True 都放在经文$k+1
的左侧$v
,否则如果!!
False""
则放置一个空字符串。请参阅this SO answer了解如何重新开始行号,例如当一首诗被分成段落/节时。
https://raku.org