[comp.editors] regexp..prepending a line globally

eli@panda.uucp (Eli Taub/100000) (07/03/90)

Cross posting from comp.unix.questions:
In article <1772@island.uu.net> daniel@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
>
Stuff 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@well.sf.ca.us   daniel@island.uu.net   unicom!daniel@pacbell.com
>ph: (415) 332 3278 (h), 491 1000 (w) disclaimer: Island's coffee was laced :-)

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

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

[ ^M stands for: <CONTROL>V <ENTER> ]

To use, simply insert the above lines into a file, say exls,
and source it like `so' in (ex) command mode:      :so exls

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

mercer@npdiss1.StPaul.NCR.COMDan Mercer) (07/04/90)

In article <3236@d75.UUCP> eli@panda.uucp (Eli Taub/100000) writes:
:Cross posting from comp.unix.questions:
:In article <1772@island.uu.net> daniel@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
:>
:Stuff 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@well.sf.ca.us   daniel@island.uu.net   unicom!daniel@pacbell.com
:>ph: (415) 332 3278 (h), 491 1000 (w) disclaimer: Island's coffee was laced :-)
:
:The real answer should be - use sed, awk and friends,
:or use `find <dir> -print' when possible.
:But since you asked for it ...:
:
:$s/$/^M:/
:g/:/s@\(.*\):@,//- s!^!\1/!@\
:d a\
:*a
:
:[ ^M stands for: <CONTROL>V <ENTER> ]
:
:To use, simply insert the above lines into a file, say exls,
:and source it like `so' in (ex) command mode:      :so exls
:
:                                                              _   |___      
:Eli Taub (512) 838-4810 | Who needs emacs when you have vi |   |     |   \  |
:                        |                                  |         |   /\/
:Contractor at (AWD) IBM | I express my opinions not IBM's. |        /   |  \


The real question was how to prepend lines.  You have to remember
the two 'anchors': '^' and '$'.  T prepend,  replace the inital
anchor with your text:

1,$s/^/\/usr\/acct\/xxxx.../


Be sure to escape the slashes.
-- 

Dan Mercer
Reply-To: mercer@npdiss1.StPaul.NCR.COM (Dan Mercer)
"MAN - the only one word oxymoron in the English Language"

witold@enme1.ucalgary.ca (Witold Jan Owoc) (07/04/90)

In article <130@npdiss1.StPaul.NCR.COM> mercer@npdiss1.StPaul.NCR.COM (Dan Mercer) writes:
>1,$s/^/\/usr\/acct\/xxxx.../
>
>
>Be sure to escape the slashes.
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1,$s;^;/usr/acct/xxx...;

Depending on setting of 'magic' you can pick up
any non-alphanumeric character instead of ; or /

witold@enme.UCalgary.CA        |   Witold Owoc
witold%enme@UNCANET.BITNET     | The University of Calgary, Canada
uunet!enme.UCalgary.CA!witold  | std. disc......

eli@panda.uucp (Eli Taub/100000) (07/07/90)

In article <130@npdiss1.StPaul.NCR.COM> mercer@npdiss1.StPaul.NCR.COM (Dan Mercer) writes:
#In article <3236@d75.UUCP> eli@panda.uucp (Eli Taub/100000) writes:
#:In article <1772@island.uu.net> daniel@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
#:            ... Stuff 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@well.sf.ca.us   daniel@island.uu.net   unicom!daniel@pacbell.com
#:
#:$s/$/^M:/
#:g/:/s@\(.*\):@,//- s!^!\1/!@\
#:d a\
#:*a
#:
#:To use, simply insert the above lines into a file, say exls,
#:and source it like `so' in (ex) command mode:      :so exls
#:    Eli Taub
#
#The real question was how to prepend lines. ...
#1,$s/^/\/usr\/acct\/xxxx.../
#
#Be sure to escape the slashes.
            ^^^^^^^^^^^^^^^^^^---> just use `!': %s!^!/usr/acct/xxxx!
#Dan Mercer
#Reply-To: mercer@npdiss1.StPaul.NCR.COM (Dan Mercer)

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 find the correct directory to prepend to EACH line.

For example on the file:    /usr:
                            bin
                            etc
                            /usr/etc:
                            rup
                            spray
                            /usr/include:
                            core.h
                            curses.h

If you source the code above you'll get:    /usr/bin
                                            /usr/etc
                                            /usr/etc/rup
                                            /usr/etc/spray
                                            /usr/include/core.h
                                            /usr/include/curses.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.      /   |  \