regexp..prepending a line globally (Vi)

Eli Taub/100000 eli at panda.uucp
Sat Jul 7 02:58:00 AEST 1990


In article <1990Jul3.155941.6037 at chinet.chi.il.us> les at chinet.chi.il.us (Leslie Mikesell) writes:
>In article <3235 at d75.UUCP> eli at reed.UUCP (Eli Taub/100000) writes:
>
>[how to do this in vi]:
>>>/usr/spool/ftp/comp.sources.unix/volume22/nn6.4:
>>>part01.Z
>>>part02.Z
>>Comments deleted ...
>
>>>What I want to end up with is:
>>>/usr/spool/ftp/comp.sources.unix/volume22/nn6.4/part01.Z
>>>/usr/spool/ftp/comp.sources.unix/volume22/nn6.4/part02.Z
>   >....
>   >But since you asked for it ...:
>   >$s/$/^M:/
>   >g/^.*:$/s @^\(.*\):$@.,/:/-1 s!^!\1/!@ \
>   >d a \
>   >*a
>
>Good grief!
>Just edit the first line into the command for the transformation, delete
>it into a register, and execute the register.  This gives you a series
>of steps you can undo if you mis-type something.
>
>edit to:
>:%s,^,/usr/spool/ftp/comp.sources.unix/volume22/nn6.4/,
>with the cursor on that line, type "add  (delete to register a)
>then @a   (execute register a)
>if it doesn't look right, just u (undo) "aP (put back from register a)
>and try again.  The only problem to watch for with this approach is
>that you need an extra level of ^V quoting for any special characters
>(one to get them into the edit buffer, one to get them into the command).
>
>Les Mikesell
>  les at chinet.chi.il.us

You are quite correct if you only had one directory to prepend to ALL
the lines, but what do you do with the output of say `ls -R /usr' ?
The above code will automaticaly find the correct directory to prepend
to each line.
For example on the file: 

/usr:
bin
etc
lib
/usr/etc:
rup
spray
/usr/include:
core.h
curses.h
errno.h

If you source the code above you'll get:
/usr/bin
/usr/etc
/usr/lib
/usr/etc/rup
/usr/etc/spray
/usr/include/core.h
/usr/include/curses.h
/usr/include/errno.h

And you can undo the whole thing with 1 `u'!!

                                                           _   |___      
Eli Taub                                                    |     |   \  |
(512) 838-4810                                                    |   /\/
Contractor at (AWD) IBM  | I express my opinions not IBM's.      /   |  \



More information about the Comp.unix.questions mailing list