Text

任何文本實用程序或破解省略某些數字的詩行編號?

  • February 8, 2022

我的目標是嘗試查看 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,

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.

每噹噹前輸入文件 ( FNR) 的行號可以被 4 整除時,列印帶有行號的行。否則以相同數量的空格縮進列印。


對於額外的縮進,嘗試:

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

%6s注意第二行後面額外的兩個空格。

並且,對於第二個縮進樣式:

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在第一行和%6s第二行之後。

引用自:https://unix.stackexchange.com/questions/680747