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

grep -o

Expand Messages
  • Alan G Isaac
    For the current buffer I d like to get the equivalent of grep -o pattern into a register. How? Thanks, Alan Isaac
    Message 1 of 9 , Jun 29, 2004
    • 0 Attachment
      For the current buffer I'd like to get the equivalent of
      grep -o pattern
      into a register. How?

      Thanks,
      Alan Isaac
    • gumnos (Tim Chase)
      ... Hideous as this is, it should do the trick: let @a= | g/foo/let @a=@a.substitute(getline(line( . )), . {-} ( .@/. ).* , 1 , g ). n (that s all
      Message 2 of 9 , Jun 29, 2004
      • 0 Attachment
        > For the current buffer I'd like to get the equivalent of
        > grep -o pattern
        > into a register. How?

        Hideous as this is, it should do the trick:

        let @a='' | g/foo/let
        @a=@...(getline(line('.')),".\\{-}\\(".@/."\\).*",'\1','g')."\n
        "

        (that's all one line, no white-space)

        that
        0) clears the register @a
        1) searches for the regexp "foo" (just about anything should work...I
        toyed with "escape()", but found that it caused problems, but when @/
        held some character like "\" that I would have expected to be taboo,
        *not* using "escape()" worked, while using "escape()" caused it to biff
        up.
        2) on each of those lines, append the found target to @a, and tack on a
        newline.

        One might be able to more kindly wrap this in a function to keep from
        having to type this monstrosity on any sort of regular basis. But it
        works on the test cases I threw at it.

        Caveat: it will only snag the first one on the line. If you prefer the
        last one on the line, change the

        \\{-}

        to an asterisk. At worst, this could be a starting ground for tweaking
        as you please.

        HTH,

        -tim
      • Alan G Isaac
        ... Hideous but cool! I m learning several things here. This gets me 90%: grep -o foo shows all occurences of foo. Thanks! Alan
        Message 3 of 9 , Jun 29, 2004
        • 0 Attachment
          >> For the current buffer I'd like to get the equivalent of
          >> grep -o pattern
          >> into a register. How?


          On Tue, 29 Jun 2004, "gumnos (Tim Chase)" apparently wrote:
          > let @a='' | g/foo/let
          > @a=@...(getline(line('.')),".\\{-}\\(".@/."\\).*",'\1','g')."\n"
          > (that's all one line, no white-space)


          Hideous but cool!
          I'm learning several things here.

          This gets me 90%:
          grep -o foo
          shows all occurences of foo.

          Thanks!
          Alan
        • Brent Rice
          It would seem to me that the following could work. ... You will have to get rid of the first line or so of the register, since it will contain the grep
          Message 4 of 9 , Jun 29, 2004
          • 0 Attachment
            It would seem to me that the following could work.

            :redir @a
            :!grep -o foo <C-R>%
            :redir END

            You will have to get rid of the first line or so of the register, since
            it will contain the grep command, but that is not too big of a
            problem. Of course, this assumes that you have access to an external
            grep program. So, perhaps not perfect, but just a thought.

            Brent


            On Jun 29, 2004, at 2:36 PM, Alan G Isaac wrote:

            >>> For the current buffer I'd like to get the equivalent of
            >>> grep -o pattern
            >>> into a register. How?
            >
            >
            > On Tue, 29 Jun 2004, "gumnos (Tim Chase)" apparently wrote:
            >> let @a='' | g/foo/let
            >> @a=@...(getline(line('.')),".\\{-}\\(".@/
            >> ."\\).*",'\1','g')."\n"
            >> (that's all one line, no white-space)
            >
            >
            > Hideous but cool!
            > I'm learning several things here.
            >
            > This gets me 90%:
            > grep -o foo
            > shows all occurences of foo.
            >
            > Thanks!
            > Alan
            >
            >
          • Alan G Isaac
            ... I don t get redirection of the shell (cmd.exe) output this way. Thanks, Alan
            Message 5 of 9 , Jun 29, 2004
            • 0 Attachment
              On Tue, 29 Jun 2004, Brent Rice apparently wrote:
              > It would seem to me that the following could work.
              > :redir @a
              > :!grep -o foo <C-R>%
              > :redir END

              I don't get redirection of the shell (cmd.exe) output
              this way.

              Thanks,
              Alan
            • gumnos (Tim Chase)
              ... @a=@a.substitute(getline(line( . )), . {-} ( .@/. ).* , 1 , g ). n ... Thanks :) ... Okay, how s this for a 2nd stab? let @a= | g/foo/let
              Message 6 of 9 , Jun 29, 2004
              • 0 Attachment
                > On Tue, 29 Jun 2004, "gumnos (Tim Chase)" apparently wrote:
                > > let @a='' | g/foo/let
                > >
                @a=@...(getline(line('.')),".\\{-}\\(".@/."\\).*",'\1','g')."\n
                "
                > > (that's all one line, no white-space)
                >
                > Hideous but cool!

                Thanks :)

                > This gets me 90%:
                > grep -o foo
                > shows all occurences of foo.

                Okay, how's this for a 2nd stab?

                let @a='' | g/foo/let
                @a=@...(getline(line('.')),".\\{-}\\(".@/."\\)\\(\\%(".@/."\\)\
                \@!.\\)*","\\1\n",'g')

                (again all one bletcherous line) I think this grabs all instances on a
                line, assuming they don't overlap.

                -tim
              • Alan G Isaac
                ... I m still parsing this, but it does seem to work. Thanks! Alan
                Message 7 of 9 , Jul 1 9:07 AM
                • 0 Attachment
                  On Tue, 29 Jun 2004, "gumnos (Tim Chase)" apparently wrote:
                  > let @a='' | g/foo/let @a=@...(getline(line('.')),".\\{-}\\(".@/."\\)\\(\\%(".@/."\\)\\@!.\\)*","\\1\n",'g')
                  > (again all one bletcherous line) I think this grabs all instances on a line, assuming they don't overlap.

                  I'm still parsing this,
                  but it does seem to work.

                  Thanks!
                  Alan
                • gumnos (Tim Chase)
                  ... @a=@a.substitute(getline(line( . )), . {-} ( .@/. ) ( %( .@/. ) @!. )* , 1 n , g ) ... on a line, assuming they don t overlap. ... Glad it
                  Message 8 of 9 , Jul 1 10:28 AM
                  • 0 Attachment
                    > > let @a='' | g/foo/let
                    @a=@...(getline(line('.')),".\\{-}\\(".@/."\\)\\(\\%(".@/."\\)\
                    \@!.\\)*","\\1\n",'g')
                    > > (again all one bletcherous line) I think this grabs all instances
                    on a line, assuming they don't overlap.
                    >
                    > I'm still parsing this,
                    > but it does seem to work.

                    Glad it serves your purpose. I'll try and explain a bit what it's doing
                    if you want to follow along. The trick was to take advantage of the "g"
                    option for "substitute()" to snag the rest of them on the line, and then
                    use the "\\@!" to prevent the search-regexp from slurping up additional
                    hits. It replaces the simple ".*" (from my first stab at the regexp,
                    which heaved the rest of the line in the bit-bucket, even if it had
                    another match) with the more complex "anything up to but not including
                    the next match" which is

                    \(\%(regexp\)\@!.\)*

                    when un-escaped. It then replaces the preceeding non-matching junk, the
                    hit, and the trailing junk with simply the hit, followed by a newline.
                    Do this for the remainder of the line ("g") which may contain further
                    hits, and you're golden :) The big trick was making sure that all the
                    escapings happened properly.

                    -tim
                  • Charles E Campbell
                    ... That s pretty good -- you know, you could eliminate a lot of backslashes if you used single quotes: let @a= |g/ab +a/let
                    Message 9 of 9 , Jul 2 6:23 PM
                    • 0 Attachment
                      gumnos (Tim Chase) wrote:

                      >Okay, how's this for a 2nd stab?
                      >
                      > let @a='' | g/foo/let
                      >@a=@...(getline(line('.')),".\\{-}\\(".@/."\\)\\(\\%(".@/."\\)\
                      >\@!.\\)*","\\1\n",'g')
                      >
                      >(again all one bletcherous line) I think this grabs all instances on a
                      >line, assuming they don't overlap.
                      >
                      >
                      >
                      That's pretty good -- you know, you could eliminate a lot of backslashes
                      if you used single quotes:
                      let @a=''|g/ab\+a/let
                      @a=@...(getline(line('.')),'.\{-}\('.@/.'\)\(\%('.@/.'\)\@!.\)*','\1\n','g')

                      When applied to
                      aba afa abba acca abbba ada abbbba

                      and doing "ap after executing the above command yields:
                      aba
                      abba
                      abbba
                      abbbba

                      which looks ok to me!

                      Regards,
                      Chip Campbell
                    Your message has been successfully submitted and would be delivered to recipients shortly.