Path: utzoo!attcan!uunet!cs.utexas.edu!ut-emx!ibmchs!auschs!d75!panda!eli From: eli@panda.uucp (Eli Taub/100000) Newsgroups: comp.unix.questions Subject: Re: regexp..prepending a line globally (Vi) Summary: Too long for one line .... Keywords: vi, ex, ed Message-ID: <3235@d75.UUCP> Date: 2 Jul 90 20:23:20 GMT References: <1772@island.uu.net> Sender: news@d75.UUCP Reply-To: eli@reed.UUCP (Eli Taub/100000) Organization: IBM AWD, Austin, TX Lines: 89 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 >. >. 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@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 ..., or use `find -print' when possible. But since you asked for it ...: $s/$/^M:/ g/^.*:$/s @^\(.*\):$@.,/:/-1 s!^!\1/!@ \ d a \ *a [ ^M stands for: V ] 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. | / | \