Description
Given a datafile contains verses from the Bible:
  • Each verse begins with the name of the book, a blank, the chapter number, followed by a colon, the verse number, and the verse itself. For example, Psm 91:1 indicates that the chapter number is 91 and the verse number is 1.
  • If a line begins with blanks, it is part of the same verse as the previous line.
If a verse is from the same book and the same chapter as the previous one, we want to replace everything up to and including the colon with blanks of the same length.
Raw Input
Psm 91:1 He who dwells in the shelter of the Most High
         will rest in the shadow of the Almighty.
Psm 91:2 I will say of the LORD, "He is my refuge and my fortress,
         my God, in whom I trust." 
Psm 127:3 Sons are a heritage from the LORD,
          children a reward from him. 
Psm 127:4 Like arrows in the hands of a warrior are sons born in one's youth. 
Psm 9:1 I will praise you, O LORD, with all my heart;
        I will tell of all your wonders. 
Psm 9:2 I will be glad and rejoice in you;
        I will sing praise to your name, O Most High. 
Psm 9:3 My enemies turn back; they stumble and perish before you. 
Desired Output
Psm 91:1 He who dwells in the shelter of the Most High
         will rest in the shadow of the Almighty.
       2 I will say of the LORD, "He is my refuge and my fortress,
         my God, in whom I trust." 
Psm 127:3 Sons are a heritage from the LORD,
          children a reward from him. 
        4 Like arrows in the hands of a warrior are sons born in one's youth. 
Psm 9:1 I will praise you, O LORD, with all my heart;
        I will tell of all your wonders. 
      2 I will be glad and rejoice in you;
        I will sing praise to your name, O Most High. 
      3 My enemies turn back; they stumble and perish before you. 
Script and Comments
Script1
[ 1] /^ +/b
[ 2] G
[ 3] s/^([^:]+:)([^\n]*)\n\1\n(.*)/\3\2/
[ 4] t
[ 5] s/\n.*//p
[ 6] s/:.*$/:/
[ 7] h
[ 8] s/./ /g
[ 9] H
[10] d
Comments -r
  1. HS contains
    • the book name, a blank, and the chapter number of the previous verse,
    • then a colon, a newline character, and
    • a sequence of blanks whose length is equal the contents before the newline character.
    For example, if the previous verse belongs to Chapter 9 of Psalm, the contents of HS are Psm 9:\n followed by six blanks. These blanks are used to replace other verses begins with Psm 9:.
  2. If a line begins with blanks (^ + matches it), it is part of the same verse as the previous line; therefore, no modification is required, and command `b' of Step [1] will:
    • makes sed jump to the end of the script,
    • print it, and start a new cycle.
  3. Otherwise, we have to check whether the book name and the chapter number is the same as the previous line:
    • First, `G' of Step [2] appends the required information to PS. After `G', PS consists of
      • the current line,
      • a newline character,
      • the book-blank-chapter-colon of previous line,
      • and a sequences of blanks of the same length, preceded by a newline character.
    • Then, Step [3] will succeed if the book-blank-chapter-colon sequence is the same as the previous line. In this case, that sequence will be replaced with the blanks of the same length. Then Step [4] will be performed: the line will be printed and then start a new cycle. No modification on HS is required.
    • Or, this line is not of the same book-chapter as the previous line. We have to
      • Print the current line, but the data resulting from `G' of Step [2] must be discarded first. These are done by Step [5].
      • Then HS has to be updated from PS. These are done via Steps [6] thru [9].
      • Processing on the current line is now finished. Step [10] discards the contents of PS and starts a new cycle.