- 论坛徽章:
- 0
|
6.5.2 Switching Items in a Database
Suppose you want to switch the order of all last names and first
names in a (text) database. The lines look like this:
Name: Feld, Ray; Areas: PC, UNIX; Phone: 123-4567Name: Joy, Susan S.; Areas: Graphics; Phone: 999-3333
The name of each field ends with a colon, and each field is
separated by a semicolon. Using the top line as an example, you
want to change Feld, Ray to Ray Feld. We'll present some
commands that look promising but don't work. After each
command, we show you the line the way it looked before the
change and after the change.
:%s/: \(.*\), \(.*\);/: \2 \1;/
Name: Feld, Ray; Areas: PC, UNIX; Phone: 123-4567Before
Name: UNIX Feld, Ray; Areas: PC; Phone: 123-4567After
We've highlighted the contents of the first hold buffer in bold and
the contents of the second hold buffer in italic. Note that the first
hold buffer contains more than you want. Since it was not
sufficiently restricted by the pattern that follows it, the hold buffer
was able to save up to the second comma. Now you try to restrict
the contents of the first hold buffer:
:%s/: \(....\), \(.*\);/: \2 \1;/
Name: Feld, Ray; Areas: PC, UNIX; Phone: 123-4567Before
Name: Ray; Areas: PC, UNIX Feld; Phone: 123-4567After
Here you've managed to save the last name in the first hold buffer,
but now the second hold buffer will save anything up to the last
semicolon on the line. Now you restrict the second hold buffer, too:
:%s/: \(....\), \(...\);/: \2 \1;/
Name: Feld, Ray; Areas: PC, UNIX; Phone: 123-4567Before
Name: Ray Feld; Areas: PC, UNIX; Phone: 123-4567After |
|