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

search and replace question

Expand Messages
  • Cory Riddell
    I have a file that I want to do a search and replace for any lines that start with two (and only two) spaces followed by a non-space character and replace it
    Message 1 of 10 , Apr 1, 2005
      I have a file that I want to do a search and replace for any lines that
      start with two (and only two) spaces followed by a non-space character
      and replace it with two spaces, an asterisk, a space, then the non-space
      character. For example, I want to replace this:
      Some Text
      and some more text
      with
      * Some Text
      * and some more text

      I tried this command
      :%s/^ ([\S])/ * \1/g

      but it just tells me the search pattern was not found. Can anybody help
      me find the right expression?

      Cory
    • Scott LaBounty
      ... Cory, Take out the square brakets and add backslashes on the parens in other ... Scott LaBounty Nexa Technologies, Inc.
      Message 2 of 10 , Apr 1, 2005
        Cory Riddell wrote:

        >I have a file that I want to do a search and replace for any lines that
        >start with two (and only two) spaces followed by a non-space character
        >and replace it with two spaces, an asterisk, a space, then the non-space
        >character. For example, I want to replace this:
        > Some Text
        > and some more text
        >with
        > * Some Text
        > * and some more text
        >
        >I tried this command
        >:%s/^ ([\S])/ * \1/g
        >
        >but it just tells me the search pattern was not found. Can anybody help
        >me find the right expression?
        >
        >Cory
        >
        >
        >
        >
        >________________________________________________________________________
        >nexatechnologies.com - Inbound email scanned for viruses by MessageLabs.
        >
        >
        >
        Cory,

        Take out the square brakets and add backslashes on the parens in other
        words:

        :%s/^ \(\S\)/ * \1/g

        Scott LaBounty
        Nexa Technologies, Inc.

        ________________________________________________________________________
        This nexatechnologies.com email has been scanned for all viruses by the MessageLabs Email Security System.
      • t. scott urban
        ... You have to escape ( and ) used for submatching (unless there is some option I m not aware of). The brackets around the non-space specifier are not
        Message 3 of 10 , Apr 1, 2005
          On Fri, 2005-04-01 at 17:51 +0000, Cory Riddell wrote:
          > I have a file that I want to do a search and replace for any lines that
          > start with two (and only two) spaces followed by a non-space character
          > and replace it with two spaces, an asterisk, a space, then the non-space
          > character. For example, I want to replace this:
          > Some Text
          > and some more text
          > with
          > * Some Text
          > * and some more text
          >
          > I tried this command
          > :%s/^ ([\S])/ * \1/g

          You have to escape ( and ) used for submatching (unless there is some
          option I'm not aware of). The brackets around the non-space specifier
          are not needed, either. Since the expression can only match once per
          line because of the beginning of line anchor, the 'g' option is not
          needed either.

          :%s/^ \(\S\)/ * \1/

          --
          t. scott urban <scottu@...>
        • Tim Chase
          ... So close, yet just enough to mess things up. You *either* want S or [^ ] . Your current expression would find lines starting with two spaces followed
          Message 4 of 10 , Apr 1, 2005
            > I tried this command
            > :%s/^ ([\S])/ * \1/g

            So close, yet just enough to mess things up. You *either* want
            "\S" or "[^ ]". Your current expression would find lines
            starting with two spaces followed by a capital S only (and maybe
            a backslash).

            Try

            :%s/^ \(\S\)/ * \1

            HTH,

            -tim
          • Cory Riddell
            Thanks all for the amazingly quick replies. Cory
            Message 5 of 10 , Apr 1, 2005
              Thanks all for the amazingly quick replies.

              Cory
            • Keith Roberts
              ... -Keith
              Message 6 of 10 , Apr 4, 2005
                >-----Original Message-----
                >From: t. scott urban [mailto:scottu@...]
                >Sent: Friday, April 01, 2005 10:01 AM
                >To: criddell@...
                >Cc: vim@...
                >Subject: Re: search and replace question
                >
                >On Fri, 2005-04-01 at 17:51 +0000, Cory Riddell wrote:
                >> I have a file that I want to do a search and replace for any lines
                >> that start with two (and only two) spaces followed by a non-space
                >> character and replace it with two spaces, an asterisk, a space, then
                >> the non-space character. For example, I want to replace this:
                >> Some Text
                >> and some more text
                >> with
                >> * Some Text
                >> * and some more text
                >>
                >> I tried this command
                >> :%s/^ ([\S])/ * \1/g
                >
                >You have to escape ( and ) used for submatching (unless there
                >is some option I'm not aware of). The brackets around the
                >non-space specifier are not needed, either. Since the
                >expression can only match once per line because of the
                >beginning of line anchor, the 'g' option is not needed either.
                >
                >:%s/^ \(\S\)/ * \1/

                :%s/^ (\S)\v/ * \1/ will also work.

                -Keith
              • jamessan@jamessan.com
                ... James -- GPG Key: 1024D/61326D40 2003-09-02 James Vega
                Message 7 of 10 , Apr 4, 2005
                  On Mon, Apr 04, 2005 at 09:59:21AM -0700, Keith Roberts wrote:
                  > >-----Original Message-----
                  > >From: t. scott urban [mailto:scottu@...]
                  > >Sent: Friday, April 01, 2005 10:01 AM
                  > >To: criddell@...
                  > >Cc: vim@...
                  > >Subject: Re: search and replace question
                  > >
                  > >On Fri, 2005-04-01 at 17:51 +0000, Cory Riddell wrote:
                  > >> I have a file that I want to do a search and replace for any lines
                  > >> that start with two (and only two) spaces followed by a non-space
                  > >> character and replace it with two spaces, an asterisk, a space, then
                  > >> the non-space character. For example, I want to replace this:
                  > >> Some Text
                  > >> and some more text
                  > >> with
                  > >> * Some Text
                  > >> * and some more text
                  > >>
                  > >> I tried this command
                  > >> :%s/^ ([\S])/ * \1/g
                  > >
                  > >You have to escape ( and ) used for submatching (unless there
                  > >is some option I'm not aware of). The brackets around the
                  > >non-space specifier are not needed, either. Since the
                  > >expression can only match once per line because of the
                  > >beginning of line anchor, the 'g' option is not needed either.
                  > >
                  > >:%s/^ \(\S\)/ * \1/
                  >
                  > :%s/^ (\S)\v/ * \1/ will also work.

                  I think you meant:

                  :%s/\v^ (\S)/ * \1/

                  James
                  --
                  GPG Key: 1024D/61326D40 2003-09-02 James Vega <jamessan@...>
                • Keith Roberts
                  ... Aha! Thanks for the correction. As I never use it, I hadn t realized that v is a positional flag (like U and E) rather than one which affects the
                  Message 8 of 10 , Apr 4, 2005
                    >-----Original Message-----
                    >From: jamessan@... [mailto:jamessan@...]
                    >Sent: Monday, April 04, 2005 9:45 AM
                    >To: vim@...
                    >Subject: Re: search and replace question
                    >
                    >On Mon, Apr 04, 2005 at 09:59:21AM -0700, Keith Roberts wrote:
                    >> >-----Original Message-----
                    >> >From: t. scott urban [mailto:scottu@...]
                    >> >Sent: Friday, April 01, 2005 10:01 AM
                    >> >To: criddell@...
                    >> >Cc: vim@...
                    >> >Subject: Re: search and replace question
                    >> >
                    >> >On Fri, 2005-04-01 at 17:51 +0000, Cory Riddell wrote:
                    >> >> I have a file that I want to do a search and replace for
                    >any lines
                    >> >> that start with two (and only two) spaces followed by a non-space
                    >> >> character and replace it with two spaces, an asterisk, a space,
                    >> >> then the non-space character. For example, I want to replace this:
                    >> >> Some Text
                    >> >> and some more text
                    >> >> with
                    >> >> * Some Text
                    >> >> * and some more text
                    >> >>
                    >> >> I tried this command
                    >> >> :%s/^ ([\S])/ * \1/g
                    >> >
                    >> >You have to escape ( and ) used for submatching (unless
                    >there is some
                    >> >option I'm not aware of). The brackets around the non-space
                    >> >specifier are not needed, either. Since the expression can only
                    >> >match once per line because of the beginning of line
                    >anchor, the 'g'
                    >> >option is not needed either.
                    >> >
                    >> >:%s/^ \(\S\)/ * \1/
                    >>
                    >> :%s/^ (\S)\v/ * \1/ will also work.
                    >
                    >I think you meant:
                    >
                    >:%s/\v^ (\S)/ * \1/
                    >
                    >James

                    Aha! Thanks for the correction. As I never use it, I hadn't realized
                    that \v is a positional flag (like \U and \E) rather than one which
                    affects the entire regex (like \c).

                    -Keith
                  • jamessan@jamessan.com
                    ... I actually didn t realize v (or c) was a positional flag. I had always just seen it/used it at the head of the pattern and noticed that the expression
                    Message 9 of 10 , Apr 4, 2005
                      On Mon, Apr 04, 2005 at 02:21:40PM -0700, Keith Roberts wrote:
                      > >-----Original Message-----
                      > >From: jamessan@... [mailto:jamessan@...]
                      > >Sent: Monday, April 04, 2005 9:45 AM
                      > >To: vim@...
                      > >Subject: Re: search and replace question
                      > >
                      > >On Mon, Apr 04, 2005 at 09:59:21AM -0700, Keith Roberts wrote:
                      > >>
                      > >> :%s/^ (\S)\v/ * \1/ will also work.
                      > >
                      > >I think you meant:
                      > >
                      > >:%s/\v^ (\S)/ * \1/
                      > >
                      > >James
                      >
                      > Aha! Thanks for the correction. As I never use it, I hadn't realized
                      > that \v is a positional flag (like \U and \E) rather than one which
                      > affects the entire regex (like \c).

                      I actually didn't realize \v (or \c) was a positional flag. I had
                      always just seen it/used it at the head of the pattern and noticed that
                      the expression you had provided didn't work when I tried it. We both
                      learned something. :)

                      James
                      --
                      GPG Key: 1024D/61326D40 2003-09-02 James Vega <jamessan@...>
                    • jamessan@jamessan.com
                      ... And if I could compose my thoughts correctly, I d actually be able to convey what it was I learned. I meant to say that I didn t realize v or c could be
                      Message 10 of 10 , Apr 4, 2005
                        On Mon, Apr 04, 2005 at 11:12:16PM -0400, jamessan@... wrote:
                        > On Mon, Apr 04, 2005 at 02:21:40PM -0700, Keith Roberts wrote:
                        > > >-----Original Message-----
                        > > >From: jamessan@... [mailto:jamessan@...]
                        > > >Sent: Monday, April 04, 2005 9:45 AM
                        > > >To: vim@...
                        > > >Subject: Re: search and replace question
                        > > >
                        > > >On Mon, Apr 04, 2005 at 09:59:21AM -0700, Keith Roberts wrote:
                        > > >>
                        > > >> :%s/^ (\S)\v/ * \1/ will also work.
                        > > >
                        > > >I think you meant:
                        > > >
                        > > >:%s/\v^ (\S)/ * \1/
                        > > >
                        > > >James
                        > >
                        > > Aha! Thanks for the correction. As I never use it, I hadn't realized
                        > > that \v is a positional flag (like \U and \E) rather than one which
                        > > affects the entire regex (like \c).
                        >
                        > I actually didn't realize \v (or \c) was a positional flag. I had
                        > always just seen it/used it at the head of the pattern and noticed that
                        > the expression you had provided didn't work when I tried it. We both
                        > learned something. :)

                        And if I could compose my thoughts correctly, I'd actually be able to
                        convey what it was I learned. I meant to say that I didn't realize \v
                        or \c could be used anywhere other than the head of the pattern.

                        James
                        --
                        GPG Key: 1024D/61326D40 2003-09-02 James Vega <jamessan@...>
                      Your message has been successfully submitted and would be delivered to recipients shortly.