カスタムルールに従って、詩の行をどんどんインデントする方法を知りたいです。例えば
次のような結果があるとします。
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行に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
最初の行だけをインデントし、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つのスペースを追加します。基準寸法4は1に等しくありません。これは、最初、4番目などを除くすべての行に対してこれを行うことを意味します。
awkではNR
行番号$0
と行の内容なので、NR % 4 != 1{$0=" "$0};
「現在行番号モジュールで4が1でない場合、行の先頭に空白4個を追加する」という意味だ。最後は1;
「印刷」の略です。
Perlでは、$.
現在の行番号で、s/old/new/
最初の項目を置き換える代替演算子です。これは、「現在の行番号モジュールで4が1以外の場合は、行の先頭()を4つのスペースに置き換えます」を意味します。 「提供されたスクリプトを適用した後、入力ファイルの各行を印刷します」を意味します。old
new
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)
自動印刷(下の最初の2つ):
raku -pe 's/^/ / if $++ % 4;'
または
raku -pe 'state $i; s/^/ / if $i++ % 4;'
または(非自動印刷、次の2つ)
raku -ne '$++ % 4 ?? put " $_" !! put "$_";'
または(非自動印刷、内部引用なし)
raku -ne 'state $i; $i++ % 4 ?? qq[ $_].put !! $_.put;'
上記のRakuコードの最初の2つの例は、-pe
@terdonのPerl(5)1行コード(フラグ)を直接翻訳したものです。 Rakuは$_
Perlと同じテーマ変数を使用します。 Rakuはstate
一度だけ初期化される変数を好み、多くの特殊変数を放棄します。一般的に使用されるstate
変数には、(ここで行番号を計算するために使用される匿名スカラー)$
などの増減形式が含まれます。$++
最後の2つの例は非自動印刷(-ne
ロゴ)であり、Rakuの??
本物 !!
間違った三項演算子。これらの形式は概念的に@Ed_Mortonのawk
コード(注釈)に似ています。デフォルトはテーマ変数です)。$_
.put
$_
put
\n
最後に、Rakuの多くの引用問題は、以下のように「Q言語」を使用して解決されます(上記の4行の使用例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/言語/quoting
https://docs.raku.org/言語/operators#index-entry-operator_ternary