regexp..prepending a line globally (Vi)

Eli Taub/100000 eli at panda.uucp
Tue Jul 3 06:23:20 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
>.
>. etc...
>
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
>
>                Daniel
>-- 
>   dansmith at well.sf.ca.us   daniel at island.uu.net   unicom!daniel at pacbell.com
>ph: (415) 332 3278 (h), 491 1000 (w) disclaimer: Island's coffee was laced :-)

The real answer should be - use sed / awk ...,
or use `find <dir> -print' when possible.
But since you asked for it ...:

$s/$/^M:/
g/^.*:$/s @^\(.*\):$@.,/:/-1 s!^!\1/!@ \
d a \
*a

[ ^M stands for: <CONTROL>V <ENTER> ]
To use, simply insert the above lines into a file (e.g. exls),
and source it like so (with `so') in ex mode:     :so exls

Explanation:

$s/$/^M:/
^^^^^^^^^
Replace last EOL with NL and `:' (add extra line at end of file with `:'),
explained why this is needed later.

g/^.*:$/s @^\(.*\):$@.,/:/-1 s!^!\1/!@ \
^^^^^^^^
For each line ending with `:' do the following:

1.  g/^.*:$/s @^\(.*\):$@.,/:/-1 s!^!\1/!@ \
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    While saving everything on the line but `:', substitute it with:
                         .,/:/-1 s!^!\1/!
    where `\1' it replaced with the saved regexp.
    For example a line looking like so `/usr/bin:' would become:
    .,/:/-1 s!^!/usr/bin/! 

    g/^.*:$/s @^\(.*\):$@.,/:/-1 s!^!\1/!@ \
                         ^^^^^^^
    On every line from current line (found by the `g' command above),
    to the line before the next line having a `:'.
    (In order for search to succeed on last directory we need to add
    a line containing `:' - see first command above.)

    g/^.*:$/s @^\(.*\):$@.,/:/-1 s!^!\1/!@ \
                                 ^^^^^^^^
    Substitute start of line (i.e. add to front of line) the regexp saved.

    g/^.*:$/s @^\(.*\):$@.,/:/-1 s!^!\1/!@ \
                                           ^
    The next line is a subcommand, to ran in same context as this command.
                                         

2.  d a \
    ^^^
    Delete the line created above and put it into the named buffer `a'.

3.  *a
    ^^
    Execute the command found in the named buffer `a' as if it were typed in.
    This will do the substitute that was deleted into the named buffer.

Comment: I use `@' and `!' for regexp delimiter so that `/' in pathnames will
         not mess up the `s' commands.



                                                              _   |___      
Eli Taub                | Who needs emacs when you have vi |   |     |   \  |
(512) 838-4810          |                                  |         |   /\/
Contractor at (AWD) IBM | I express my opinions not IBM's. |        /   |  \



More information about the Comp.unix.questions mailing list