0

My goal is try to see if there are common text utilities in linux that can be used to number lines of poems like this:

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.

Obtaining this:

        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.

Can, awk, also indent lines in this way

          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,

And like this too?

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

2 Answers 2

1

awk is probably the most appropriate tool for this.

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

Whenever the line number of the current input file (FNR) is divisible by 4, print the line with a line number. Otherwise print it indented with the same number of spaces.


For the extra indentation, try:

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

Note the extra two spaces after %6s on the second line.

And, for the second indent style:

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

Here the extra spaces are after %6i on the first line and %6s on the second.

7
  • Very good. By variating FNR % 4 parameter, I can get numbering by 3, 4, 5 or whatever else. Can awk also indent lines? (See my original question updated)
    – Dingo
    Dec 9, 2021 at 17:30
  • 1
    Sure, if you can come up with some test to match the lines you want the extra indentation to occur on, but not other lines you don't want to indentation on.
    – cas
    Dec 10, 2021 at 1:56
  • 2
    note that it's bad form on SE to move the goalposts after a question has been answered. In general, if you have a new question, ask a separate question.
    – cas
    Dec 10, 2021 at 2:02
  • sorry. thanks for further tips
    – Dingo
    Dec 10, 2021 at 15:09
  • 1
    See man printf & experiment with the format string. The fmt strings contain either %6i or %6s, 2 or 4 spaces, and then %s. That means a 6 character wide field for integers (%6i) or strings (%s6), some spaces, then a field for a string of any length. Note that that's a minimum field width, wider numbers or strings will still be printed, taking more than the reserved space (which will affect the neat alignment). Also note that numeric fields are right-aligned by default, if you want left-aligned use negative numbers (e.g. %-6i instead of %6i). Strings are left aligned by default.
    – cas
    Dec 11, 2021 at 3:31
1

Using Raku (formerly known as Perl_6)

raku -e 'my @lines1 = lines>>.trim; for @lines1.kv -> $k,$v { put ($k+1) %% 4  \
         ?? sprintf("%6s  ", $k+1) ~ $v !! sprintf("%6s  ", "") ~ $v};'  file

SAMPLE INPUT:

Paradise Lost, by John Milton (excerpted):

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.

SAMPLE OUTPUT:

Paradise Lost, by John Milton (excerpted):

    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.

Above is an answer coded in Raku, a member of the Perl-family of programming languages. Briefly, the poem is read in using Raku's lines routine (which autochomps), and each line is trim-med. The data is stored in the @lines1 array.

In the next statement (a for loop), the @lines1 array is converted to kv a key-value pair, and iterated over. The loop's block implements Raku's ternary operator. Every occurrence of ($k+1) %% 4 if ?? True places $k+1 to the left of the $v verse, otherwise if !! False an "" empty-string is placed instead.

See this SO answer for how to restart line-numbering, e.g. when a poem is broken into paragraphs/stanzas.

https://raku.org

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.