这与这里给出的非常有趣的答案有关:任何文本实用程序或黑客对省略某些数字的诗行进行编号?
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,
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.
我想知道是否可以在空格后重新开始对任意四行进行行编号,以便:
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
$ 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.
或者如果您愿意:
$ 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.
答案2
使用乐(以前称为 Perl_6)
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
样本输入:
失乐园,作者:约翰·弥尔顿(摘录):
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.
样本输出:
失乐园,作者:约翰·弥尔顿(摘录):
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(Perl 编程语言家族的成员)编写的答案。简而言之,这首诗在出现 2 个或更多连续换行符时被slurp
编辑,将数据存储在数组中(这实质上将这首诗分成了“段落”)。在第二个语句中,超级运算符用于使用右侧的函数修改左侧的每个元素。因此链被读取,split
\n
@para
>>
@para>>.lines>>.trim
“`@para 的每个元素都被转换为行(即 chomped),@para 的每一行都被转换为修剪行”。
在下一个语句(for
循环)中,@para
将迭代该数组。每个(段落)元素立即转换为kv
键值对,并使用嵌套for
循环进行迭代,这实现了 Raku 的三元运算符。每次出现($k+1) %% 4
if ??
True 时都会放置$k+1
在该节的左侧$v
,否则如果False则放置!!
一个空字符串。""
最后,@para
再次用\n
换行符分隔。
答案3
如果您的目标是仅在至少有两个空行时重置计数器,那么:
awk '{ if(/^$/&& pre==$0)c=0; else c++;
pre=$0;
printf "%6s %s\n", (c&&c%4==0?c:"") , $0
}' infile