The transform command, y (Section 34.13), acts on the entire contents of the pattern space. It is something of a chore to do a letter-by-letter transformation of a portion of the line, but it is possible (though convoluted) as the following example demonstrates. [The real importance of this example is probably not the use of the y command, but the use of the hold space to isolate and preserve part of the line. — TOR]
While working on a programming guide, we found that the names of statements were entered inconsistently. They needed to be uppercase, but some were lowercase while others had an initial capital letter. While the task was simple — to capitalize the name of the statement — there were nearly a hundred statements and it seemed a tedious project to write that many explicit substitutions of the form:
s/find the Match statement/find the MATCH statement/g
The transform command could do the lowercase-to-uppercase conversion, but it applies the conversion to the entire line. The hold space makes this task possible because we use it to store a copy of the input line while we isolate and convert the statement name in the pattern space. Look at the script first:
# capitalize statement names /the .* statement/{ h s/.*the \(.*\) statement.*/\1/ y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/ G s/\(.*\)\n\(.*the \).*\( statement.*\)/\2\1\3/ }
The address limits the procedure to lines that match the .* statement
. Let's look at what each command does:
h
The hold command copies the current input line into the hold
space. Using the sample line find the Match
statement
, we'll show what the contents of the pattern
space and hold space contain. After the h
command, the pattern space and hold space are identical.
s/.*the \(.*\)
statement.*/\1/
The substitute command extracts the name of the statement from the line and replaces the entire line with it.
y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/
The transform command changes each lowercase letter to an uppercase letter.
G
The
Get command appends the line
saved in the hold space to the pattern space. The embedded newline
from the Get command is shown as \n
.
s/\(.*\)\n\(.*the \).*\(
statement.*\)/\2\1\3/
The substitute command matches three different parts of the
pattern space: (1) all characters up to the embedded newline, (2)
all characters following the embedded newline and up to and
including the
followed by a
space, and (3) all characters beginning with a space and followed by
statement
up to the end of
the pattern space. The name of the statement as it appeared in the
original line is matched but not saved. The replacement section of
this command recalls the saved portions and reassembles them in a
different order, putting the capitalized name of the command in
between the
and statement
.
Let's look at a test run. Here's our sample file:
find the Match statement Consult the Get statement. using the Read statement to retrieve data
Running the script on the sample file produces:
find the MATCH statement Consult the GET statement. using the READ statement to retrieve data
As you can see from this script, the hold space can be skillfully used to isolate and manipulate portions of the input line.
— DD