与えられた規則に従ってテキスト行をインデントします。

与えられた規則に従ってテキスト行をインデントします。

カスタムルールに従って、詩の行をどんどんインデントする方法を知りたいです。例えば

次のような結果があるとします。

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つのスペースに置き換えます」を意味します。 「提供されたスクリプトを適用した後、入力ファイルの各行を印刷します」を意味します。oldnews/^/ / 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

おすすめ記事