行番号付けを再開し、スペースの後ろの4行に番号を付けます。

行番号付けを再開し、スペースの後ろの4行に番号を付けます。

これはここに与えられた非常に興味深い答えに関連しています。特定の数字を省略する時の行番号付けのためのテキストユーティリティやハッキングはありますか?

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,
    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.

空白の後ろの4行について行番号付けを再開できるかどうか疑問に思います。

    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
 4  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
 8  Things unattempted yet in Prose or Rhime.
    

ベストアンサー1

$ awk '
    !NF { c=0 }
    NF {
        sub(/^ +/,"")
        $0 = sprintf("%2s  %s", (++c%4 ? "" : c), $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,
 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
 4  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
 8  Things unattempted yet in Prose or Rhime.

または必要に応じて:

$ awk -v RS= -F'\n' '
    NR>1 { print ORS }
    {
        for ( i=1; i<=NF; i++) {
            sub(/^ +/,"",$i)
            printf "%2s  %s\n", (i%4 ? "" : i), $i
        }
    }
' file
    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
 4  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
 8  Things unattempted yet in Prose or Rhime.

おすすめ記事