- 论坛徽章:
- 0
|
In your sample database, the last names end with a comma, so a
last-name field can be thought of as a string of characters that are
not commas:
[^,]*
This pattern matches characters up until the first comma. Similarly,
the first-name field is a string of characters that are not semicolons:
[^;]*
Putting these more efficient patterns back into your previous
command, you get:
:%s/: \([^,]*\), \([^;]*\);/: \2 \1;/
The same command could also be entered as a context-sensitive
replacement. If all lines begin with Name, you can say:
:g/^Name/s/: \([^,]*\), \([^;]*\);/: \2 \1;/
You can also add an asterisk after the first space, in order to match
a colon that has extra spaces (or no spaces) after it:
:g/^Name/s/: *\([^,]*\), \([^;]*\);/: \2 \1;/
6.5.3 Using :g to Repeat a Command
As we've usually seen the :g command used, it selects lines that
are typically then edited by subsequent commands on the same
line鈥攆or example, we select lines with g, and then make
substitutions on them, or select them and delete them:
:g/mg[ira]box/s/box/square/g:g/^$/d
However, in his two-part tutorial in UNIX World,[9] Walter Zintz
makes an interesting point about the g command. This command
selects lines鈥攂ut the associated editing commands need not
actually affect the lines that are selected.
[9] Part 1, "vi Tips for Power Users," appears in the April 1990 issue of UNIX World. Part 2, "Using vi to
Automate Complex Edits," appears in the May 1990 issue. The examples presented are from Part 2.
Instead, he demonstrates a technique by which you can repeat ex
commands some arbitrary number of times. For example, suppose
you want to place ten copies of lines 12 through 17 of your file at
the end of your current file. You could type: |
|