regexp..prepending a line globally (Vi)

John Macdonald jmm at eci386.uucp
Wed Jul 4 01:34:05 AEST 1990


In article <1772 at island.uu.net> daniel at island.uu.net (Daniel Smith - OPD Gang) writes:
|
|	All my years in vi and this one stumps me!  I wanted
|to make a real quick shell script, taken from the output
|of an ls-lR on uunet.  So, I get these lines:
|
|/usr/spool/ftp/comp.sources.unix/volume22/nn6.4:
|part01.Z
|part02.Z
|part03.Z
|part04.Z
|.
|.
|. etc...
|
|	So far so good.  Now what I wanted to do is take the first
|line (with the path) and prepend it to every line with a regexp.

Assuming that your implication that there is only one of these
series present (i.e. only line 1 has a path), an answer is to
use the ex side of vi (use Q if you are in vi mode to get to
ex mode, or else precede each line with a colon):

    3,$g/^/1co.-1               (duplicate the path line in front of all files)
    g/:$/join                   (join each pair)
    %s;: ;/;                    (change the : to a slash)

However, if you have more than one of the path lines mixed in, then
this requires much more contorted effort to do - it is easier to use
recursive invokations of this technique than to try to get it to work
all at once...

(again from ex - assuming that the above script is in file "joinup"):

    $a                          (ensure that there is always one more colon)
    :
    .
    g/:/.,/:/-1! ex <joinup     (for each path range, run the previous script)
    $d                          (get rid of the extra colon line)

(In fact, I would probably write a Perl script to do this instead, except
that Randall has beaten me to it...)
-- 
Algol 60 was an improvment on most           | John Macdonald
of its successors - C.A.R. Hoare             |   jmm at eci386



More information about the Comp.unix.questions mailing list