One straightforward solution is:
global /^CHAPTER/ mark a | copy $ | 'a substitute /APTER/apter/
which marks the original line, then copies it in its all-caps version to the end of the file, and finally returns to the original line to run the decapitalizing substitution.
Another, not quite so obvious but just as good, is to decapitalize "CHAPTER" first, then copy the decapitalized version to the end of the file, and finally run a substitution command on the current line (which is now the copied line at the end of the file) which changes the capitalization back to "CHAPTER".
troff
ProblemThe command line:
1 , 16 global /^/ 217 substitute /n(PDu/\\n(PDu/
does it by running the substitution command 16 times. Each time it inserts a single backslash. (The double backslash in the replacement pattern is necessary because the backslash is a special character even there.)
My solution to this problem has an intermediate stage in which each macro is followed by a string of capital I letters on the same line. The count of the capital I letters on any macro line is equal to the paragraph number. That is, the macro line for the fifth paragraph looks like this in the intermediate stage:
.ppIIIII
The paragraphs can be numbered with just two global
commands. The first one:
global /^\.pp/ . , $ substitute //.ppI/
goes to each line beginning with a start-of-paragraph macro, then
runs a substitute
command from that line through the end
of the file that puts a capital letter I after each such macro. So the
substitute
command that runs from the first marked line
puts an I after every one of the macros; from the second marked line it
puts an I after every such macro except the first; from the third marked
line it puts an I after every such macro except the first and the second;
and so on. Thus, after this global
finishes, you have a
string of the letter I after every macro that is equal in number to the
paragraph's number. That is, after the macro for the third paragraph you
have the string "III"; after paragraph 5 you have the string "IIIII";
etcetera. Already you have Roman numerals (of a very primitive sort)
numbering the paragraphs.
A second global command puts those Roman numerals into canonical form:
global /^\.pp/ substitute /IIIII/V/ g \ | substitute /VV/X/ g | substitute /IIII/IV/
(As you'll learn in the next installment of this tutorial, a lower-case
letter g at the end of a substitute
command tells the
editor to perform the substitution as many times as it can on each line,
and a backslash at the end of a partial command line means the next line
continues the command string.)
To see how our second global
command sets things right,
consider the case of the 19th paragraph. The next four lines show what
the macro line looks like at the start of the command and how it has
changed after each of the three substitute
commands has
done its work:
.ppIIIIIIIIIIIIIIIIIII .ppVVVIIII .ppXVIIII .ppXVIV
(Astute readers will realize that the paragraphs could have been
numbered with just one global
command. Each macro line
has all the capital I letters it will get before global
leaves it for the next line. So we could have had the command string
start by marking the line, next run the substitution that adds a capital
I to all remaining macro lines, then return to the line and run the
substitutions that produce a true Roman numeral.)