遵循给定规则缩进文本行

遵循给定规则缩进文本行

我想知道如何按照自定义规则递归地缩进越来越多的诗行。例如

假设我们有:

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.

我们想要递归地缩进,在第一行之后的任意三行添加 3 个空格,这样

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

实现这一目标的最简单方法是什么?

答案1

如果您只想缩进第一行,然后每第四行缩进一次,您可以使用awk


$ awk 'NR % 4 != 1{$0="    "$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,
    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.

或者perl

$ perl -pe 's/^/    / if $. % 4 != 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,
    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.

在这两种情况下,如果当前行号,我们都会在行首添加 4 个空格模块4 不等于 1,这意味着我们将对除第 1、第 4 等之外的所有行执行此操作。

在awk中,NR是行号,$0是该行的内容,因此NR % 4 != 1{$0=" "$0};表示“当当前行号模4不等于1时,在行首添加4个空格”。最后的1;只是“print”的简写。

在 Perl 中,$.是当前行号,s/old/new/是替换运算符,它将替换第一次出现oldnew。 So 的意思是“如果当前行号模 4 不等于 1,则用四个空格s/^/ / if $. % 4 != 1替换行 ( ) 的开头”。^意思-p是“在应用由”提供的脚本后打印输入文件的每一行-e

下面是完全相同的 perl 命令,但有更详细且更容易理解的版本:

perl -e '
open(my $fileHandle, "<", $ARGV[0]);
my $lineCount=0;

while(my $line = <$fileHandle>){
   $lineCount += 1;
   if ( $lineCount % 4 != 1 ){
       ## or $line = "    " . $line
       $line =~ s/^/    /
   }
   print "$line";
}' file

或者,几乎相同:

perl -e '
open(my $fileHandle, "<", $ARGV[0]);
my $lineCount=0;

while(my $line = <$fileHandle>){
   $lineCount += 1;
   unless ( $lineCount % 4 == 1 ){
       $line = "    " . $line
   }
   print "$line";
}' file

答案2

使用(以前称为 Perl_6)

自动打印(下面前两个):

raku -pe 's/^/    / if $++ % 4;'  

或者

raku -pe 'state $i; s/^/    / if $i++ % 4;'  

或(非自动打印,下面两个)

raku -ne '$++ % 4 ?? put "    $_" !! put "$_";' 

OR(非自动打印,无内部引号)

raku -ne 'state $i; $i++ % 4 ?? qq[    $_].put !! $_.put;' 

上面的 Raku 代码,前两个示例是 @terdon 的 Perl(5) 单行代码(-pe标志)的直接翻译。 Raku 使用$_主题变量——与 Perl 相同。注意 Raku 放弃了许多特殊变量,转而使用state仅初始化一次的变量。常用的state变量包括$递增/递减形式,例如$++(匿名标量,这里用于计算行号)。

最后两个例子是非自动打印(-ne标志),并使用 Raku 的“?? 真的 !! 错误的" 三元运算符。这些形式在概念上类似于 @Ed_Morton 的awk代码(注释)。尝试在 Raku 三元的每一半中保留并行文本,但实际上,输出$_主题变量时您需要做的就是编写.put(Raku如果没有明确说明,则默认为$_主题变量)。另外,put在 Raku 中会\n为您添加换行符。

最后,Raku 中的许多引用问题都是使用“Q 语言”解决的,如下所示(上面第四行中的示例使用qq[…])。

示例输出(上面所有 4 行):

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.

https://docs.raku.org/syntax/state
https://docs.raku.org/language/quoting
https://docs.raku.org/language/operators#index-entry-operator_ternary

相关内容