Loading ...
Sorry, an error occurred while loading the content.

Re: substitution

Expand Messages
  • brian
    ... Thank you! The r worked perfectly. -- ~ `^` ~=-.,__,.-=~ `^` ~=-.,__,.-=~ `^` ~=-., |/ (___) |/ _,.-=~ `^` Brian Medley @~./ O o` .~@
    Message 1 of 9 , May 1, 2001
    • 0 Attachment
      On Sun, 29 Apr 2001, Gary Johnson wrote:

      > On Sun, Apr 29, 2001 at 03:26:53PM -0500, brian wrote:
      >
      > > I would like to place a command inside of a register and execute it. The
      > > command in question is substitute. I am having some difficulties because
      > > the replace pattern has emebedded ^M's in it. An example follows.
      > >
      > > *** begin text ***
      > >:s/abc/def^Maa/
      > > abc
      > > *** end text ***
      > >
      > > I then do the following
      > > a) yank line 1 into register a
      > > b) put cursor at line 2
      > > c) type "@a"
      > >
      > > The behavior is as if the substitute ends at the ^M and the "aa/" is typed
      > > literally. How can I make the substitute include the ^Maa/?
      >
      > Here are two ways to do it.
      >
      > 1. Type ^V^M instead of just ^M.
      > 2. Type \r instead of ^M.

      Thank you! The \r worked perfectly.

      --
      ~'`^`'~=-.,__,.-=~'`^`'~=-.,__,.-=~'`^`'~=-., \|/ (___) \|/ _,.-=~'`^`
      Brian Medley @~./'O o`\.~@
      "Knowledge is Power" bmedley@... /__( \___/ )__\ *PPPFFBT!*
      -- Francis Bacon `\__`U_/'
      _,.-=~'`^`'~=-.,__,.-=~'`^`'~=-.,__,.-=~'`^`'~= <____|' ^^`'~=-.,__,.-=
      ~`'^`'~=-.,__,.-=~'`^`'~=-.,__,.-=~'`^`'~=-.,__,.-==--^'~=-.,__,.-=~'`^`
    • Matt
      I want to perform the following operation on the whole file: it seems so vim-like but I can t figure out how to do it. on a line that matches ;
      Message 2 of 9 , Jul 5 12:09 PM
      • 0 Attachment
        I want to perform the following operation on the whole file: it seems so
        vim-like but I can't figure out how to do it.

        on a line that matches <pattern1>; replace <pattern2> with a string
        containing whatever the match was for pattern1...

        example

        hello; my name is matt berardi.

        pattern1 /is [a-z]*;/ and pattern2 is /[a-z]*./ and the replace string
        is /&:<first match>/

        so that the result would be

        hello; my name is matt berardi.:hello;

        and if the next line was

        salut; your name is jose.

        the result would be

        salut; your name is jose.:salut;

        so anyway; how would you go about specifying the matched first string as
        part of the substitution; there has to be a way...

        thanks
        matt
      • Colin Keith
        ... Yup, the :g// and the :s/// commands are your friends here. ... hi :) ... Except this doesn t match your examples. The second pattern acn t be [a-z]*
        Message 3 of 9 , Jul 5 12:37 PM
        • 0 Attachment
          On Fri, Jul 05, 2002 at 03:09:28PM -0400, Matt wrote:
          > on a line that matches <pattern1>; replace <pattern2> with a string
          > containing whatever the match was for pattern1...

          Yup, the :g// and the :s/// commands are your friends here.

          > hello; my name is matt berardi.

          hi :)

          > pattern1 /is [a-z]*;/ and pattern2 is /[a-z]*./ and the replace string
          > is /&:<first match>/

          Except this doesn't match your examples. The second pattern acn't be
          [a-z]* because you have white spaces in your text...

          > so that the result would be
          >
          > hello; my name is matt berardi.:hello;

          No, pattern2 doesn't match, but we can work around that.

          :%g/^\(\l*;\).*\(\l*\).$/s/^\(\l*;\).*\(\l*\).$/&:\1/

          But you don't really need the %g as you're doing a match which
          is quite explicit. You'll find that just the s/// part would
          work fine on its own:

          :%s/^\(\l*;\).*\l*\.$/&:\1/



          hello; my name is matt berardi.
          salut; your name is jose.


          hello; my name is matt berardi.:hello;
          salut; your name is jose.:salut;


          :% - every line of the file
          s - searches
          ^\(\l*;\) - match anchored at the beginning of the line, matches
          a-z 0+ times followed by a ;. This will match ^;, so
          you may want to change the * to a \+ (1+ atoms) the
          match is captured into \1 or submatch(1)
          .* - any character 0+ times
          \l*\.$ - match anchored at the end of the line where you have
          a-z 0+ times followed by a period.

          & - the entire matching line (Also accessed as submatch(0) IIRC)
          :\1 - : followed by match #1


          > so anyway; how would you go about specifying the matched first string as
          > part of the substitution; there has to be a way...

          \1 or submatch(1) if using extended search and replaces.

          Colin.
        • Andrzej M. Ostruszka
          ... Hhhmmm, I m not sure I understand what you want but ... I think you want pattern2 to start after ; otherwise pattern2 will match helo instead of my
          Message 4 of 9 , Jul 5 1:00 PM
          • 0 Attachment
            On Fri, Jul 05 (2002), Matt wrote:
            > on a line that matches <pattern1>; replace <pattern2> with a string
            > containing whatever the match was for pattern1...

            Hhhmmm, I'm not sure I understand what you want but

            > hello; my name is matt berardi.
            >
            > pattern1 /is [a-z]*;/ and pattern2 is /[a-z]*./ and the replace string
            > is /&:<first match>/
            >
            > so that the result would be
            >
            > hello; my name is matt berardi.:hello;

            I think you want pattern2 to start after ';' otherwise pattern2 will
            match 'helo' instead of ' my name is matt berardi.' (I assume that
            you've made mistake in regex describing pattern2 -- [a-z]*. in this
            example will really match 'my ' :))

            To solve the very this problem I would use (I assume that you really
            want lowercase only)

            %s/\v(\l+;)(.*)/\1\2:\1/

            > so anyway; how would you go about specifying the matched first string as
            > part of the substitution; there has to be a way...

            Depending on what you mean by first matched string:
            - if you mean just string that was matched previously in the same
            substitute command then you just make use of the \0, \1, ...
            (see he: sub-replace-special)
            - if you mean matched string by previous separate search command then
            you would have to do substitute command s//.../ where in "..."
            you would specify replacement/expression which would copy the matched
            string somewhere and afterwards you could make another subs which
            would use this matched string
            Best regards

            PS. Sorry for any English flaws - I have no courage to check the
            grammar :).
            --
            ____ _ ___
            / | \_/ |/ _ \ Andrzej Marek Ostruszka
            / _ | | (_) | Instytut Fizyki, Uniwersytet Jagiellonski (Cracow)
            /_/ L|_|V|_|\___/ (PGP <-- finger ostruszk@...)
          • Benji Fisher
            ... I will assume that you meant to say, pattern1 is /[a-z]*;/ and pattern2 is /[a-z]* ./ and the replace string is /&: / ... I can think of two
            Message 5 of 9 , Jul 7 8:18 PM
            • 0 Attachment
              On Friday, July 5, 2002, at 03:09 PM, Matt wrote:

              > I want to perform the following operation on the whole file: it seems
              > so vim-like but I can't figure out how to do it.
              >
              > on a line that matches <pattern1>; replace <pattern2> with a string
              > containing whatever the match was for pattern1...
              >
              > example
              >
              > hello; my name is matt berardi.
              >
              > pattern1 /is [a-z]*;/ and pattern2 is /[a-z]*./ and the replace string
              > is /&:<first match>/

              I will assume that you meant to say, "pattern1 is /[a-z]*;/ and
              pattern2 is /[a-z]*\./ and the replace string is /&:<first match>/

              > so that the result would be
              >
              > hello; my name is matt berardi.:hello;
              >
              > and if the next line was
              >
              > salut; your name is jose.
              >
              > the result would be
              >
              > salut; your name is jose.:salut;
              >
              > so anyway; how would you go about specifying the matched first string
              > as part of the substitution; there has to be a way...
              >
              > thanks
              > matt

              I can think of two ways. First, if pattern1 is supposed to come
              before pattern2 in the line, you could put pattern1 inside a \(group\)
              and refer to it as \1:

              :%s/\(\l*;\)\{-}\l*\./&:\1

              Here '\l' is the same as '[a-z]'; '.\{-}' matches anything, but as
              little as possible (like '.*' but not greedy); and '\1' refers to the
              first \(group\).

              :help sub-replace-special
              :help /\l
              :help /\{-

              The second method works if you allow pattern2 to come before
              pattern1. The idea is to use :g/pattern1/ to do a substitute on each
              line matching pattern1. For each line, use matchstr() to get the part
              matching pattern1 and \= to concatenate the two pieces; in \={expr}, use
              submatch(0) instead of '&'. Here it is (untested):

              :g/\l*;/s/\l*\./\=\submatch(0) . matchstr(getline("."), '\l*;')

              :help sub-replace-special
              :help matchstr()

              HTH --Benji Fisher
            Your message has been successfully submitted and would be delivered to recipients shortly.