home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 34.13 Hold Space: The Set-Aside Buffer Chapter 34
The sed Stream Editor
Next: 34.15 Making Edits Across Line Boundaries
 

34.14 Transforming Part of a Line

The transform command, y ( 34.12 ) , 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 will demonstrate. [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, both the pattern space and the hold space are identical.

Pattern space : find the Match statement
Hold space : find the Match statement

s/.*the \(.*\) statement.*/\1/

The substitute command extracts the name of the statement from the line and replaces the entire line with it.

Pattern space : Match
Hold space : find the Match statement

y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/

The transform command changes each lowercase letter to an uppercase letter.

Pattern space : MATCH
Hold space : find the Match statement

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 .

Pattern space : MATCH\nfind the Match statement
Hold space : find the Match statement

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 .

Pattern space : find the MATCH statement
Hold space : find the Match 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 from O'Reilly & Associates' sed & awk


Previous: 34.13 Hold Space: The Set-Aside Buffer UNIX Power Tools Next: 34.15 Making Edits Across Line Boundaries
34.13 Hold Space: The Set-Aside Buffer Book Index 34.15 Making Edits Across Line Boundaries

The UNIX CD Bookshelf Navigation The UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System