特定の数字を省略する時の行番号付けのためのテキストユーティリティやハッキングはありますか?

特定の数字を省略する時の行番号付けのためのテキストユーティリティやハッキングはありますか?

私の目標は、次のように詩の行番号を付けるために使用できる一般的なテキストユーティリティがLinuxにあることを確認することです。

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
Dove-like satst brooding on the vast Abyss
And mad'st it pregnant: What in me is dark
Illumin, what is low raise and support;
That to the highth of this great Argument
I may assert Eternal Providence,
And justifie the wayes of God to men.

これを得ました:

        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
    12  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
    16  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
    20  Wast present, and with mighty wings outspread
        Dove-like satst brooding on the vast Abyss
        And mad'st it pregnant: What in me is dark
        Illumin, what is low raise and support;
    24  That to the highth of this great Argument
        I may assert Eternal Providence,
        And justifie the wayes of God to men.

awkもこのように行をインデントできます

          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,

こんなこともありますか?

        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,

ベストアンサー1

awkおそらく最も適切なツールです。

$ 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,
     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
    12  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
    16  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
    20  Wast present, and with mighty wings outspread
        Dove-like satst brooding on the vast Abyss
        And mad'st it pregnant: What in me is dark
        Illumin, what is low raise and support;
    24  That to the highth of this great Argument
        I may assert Eternal Providence,
        And justifie the wayes of God to men.

現在の入力ファイル()の行番号がFNR4で割られるたびに、行番号を使用して行を印刷します。それ以外の場合は、同じ数のスペースをインデントして印刷します。


さらにインデントするには、以下を試してください。

awk 'FNR % 4 == 0 { printf "%6i  %s\n", FNR, $0 ; next };
     NR % 4 == 1 || NR % 4 == 3 { printf "%6s    %s\n", "", $0 ; next };
     {printf "%6s  %s\n", "", $0}' poem.txt

%6s2行目の後にさらに2つのスペースがあることを確認してください。

そして2番目のインデントスタイルの場合:

awk 'FNR % 4 == 0 { printf "%6i    %s\n", FNR, $0 ; next };
     FNR % 4 == 2 { printf "%6s    %s\n",  "", $0 ; next };
     {printf "%6s  %s\n", "", $0}' poem.txt

ここで、追加のスペースは%6i最初の行と%6s2番目の行の後にあります。

おすすめ記事