我想知道如何递归地缩进越来越多的诗行,遵循一条习惯规则。例如
假设我们有:
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实现这一目标最简单的方法是什么?
发布于 2022-06-12 11:20:22
如果您只想缩进第一行,然后再缩进之后的第4行,可以使用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不等于1,我们将在行的开头添加4个空格,这意味着我们将对除第1、第4等行之外的所有行添加4个空格。
在awk中,NR是行号,$0是行的内容,因此NR % 4 != 1{$0=" "$0};的意思是“当当前行号模4不等于1时,在行的开头添加4个空格”。最后一个1;只是“打印”的缩写。
在Perl中,$.是当前的行号,而s/old/new/是代操作符,它将用new替换old的第一次出现。因此,s/^/ / if $. % 4 != 1的意思是“如果当前行数模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发布于 2022-06-15 02:23:41
使用Raku (以前称为Perl_6)
自动冲洗(以下前两项):
raku -pe 's/^/ / if $++ % 4;' 或
raku -pe 'state $i; s/^/ / if $i++ % 4;' 或者(非自动冲洗,下面两个)
raku -ne '$++ % 4 ?? put " $_" !! put "$_";' 或者(不自动冲洗,没有内部引号)
raku -ne 'state $i; $i++ % 4 ?? qq[ $_].put !! $_.put;' 上面的Raku代码,前两个例子是@terdon的Perl(5)一行代码(-pe标志)的直接翻译。Raku使用$_作为主题变量--与Perl相同。注意,Raku为state变量提供了一些特殊的变量,这些变量只初始化一次。常用的state变量包括$和增量/递减形式,如$++ (匿名标量,此处用于计数行号)。
最后两个例子是非自动清洗(-ne标志),并使用Raku的"?? True !! False“三元操作符。这些表单在概念上类似于@Ed的awk代码(注释)。尝试将并行文本保留在Raku的三元结构的每一半中,但实际上,当输出$_主题变量时,您所需要做的就是编写.put (Raku默认为$_主题变量,如果没有明确声明)。另外,Raku中的put为您添加了\n换行符。
最后,使用“q-语言”解决了Raku中的许多引用问题,如下所示(例如上面的第四行使用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_三元
https://unix.stackexchange.com/questions/705890
复制相似问题