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

Search and write

Expand Messages
  • Lars Iselid
    If you have a search statement something like this: /[0-9] And want to write :w the matching content to a file. How to do? I ve seen examples similar to this:
    Message 1 of 11 , Aug 31 5:23 AM
    • 0 Attachment
      If you have a search statement something like this:

      /[0-9]

      And want to write :w the matching content to a file. How to do? I've
      seen examples similar to this:

      /[0-9] . w >> my.txt

      But exactly this doesn't work for me.

      --
      You received this message from the "vim_use" maillist.
      Do not top-post! Type your reply below the text you are replying to.
      For more information, visit http://www.vim.org/maillist.php
    • Tim Chase
      ... Depends on whether you want to write just the match, or the entire line. If you want the entire line containing the match(es), it s pretty easy:
      Message 2 of 11 , Aug 31 6:44 AM
      • 0 Attachment
        On 08/31/11 07:23, Lars Iselid wrote:
        > If you have a search statement something like this:
        >
        > /[0-9]
        >
        > And want to write :w the matching content to a file. How to do? I've
        > seen examples similar to this:
        >
        > /[0-9] . w>> my.txt

        Depends on whether you want to write just the match, or the
        entire line. If you want the entire line containing the
        match(es), it's pretty easy:

        g/[0-9]/w>> my.txt

        If you just want to write those digits to the file, it's more
        complex. Something like:

        g/[0-9]/t.|s/[^0-9]\+//g|w! >> my.txt | d

        which copies each matching line to the line below, deletes all
        the non-digits, writes/appends the temp-line to the file, and
        then deletes the temp-line.

        -tim


        --
        You received this message from the "vim_use" maillist.
        Do not top-post! Type your reply below the text you are replying to.
        For more information, visit http://www.vim.org/maillist.php
      • Lars Iselid
        Thanks for help! I want to write the digits and actually my full regex in vim is: /[-0-9] {13,17 } I want numbers with 13 to 17 digits even if they have
        Message 3 of 11 , Aug 31 11:10 PM
        • 0 Attachment
          Thanks for help!

          I want to write the digits and actually my full regex in vim is:
          /[-0-9]\{13,17\}

          I want numbers with 13 to 17 digits even if they have hyphens between the digits. Should then be something like:

          :g/[-0-9]\{13,17\}/t.|s/[^0-9]\+//g|w! >> my.txt | d

          I tried this but I get the whole text not just numerical digits (with hyphens) in a new file.

          Lars




          On Wed, Aug 31, 2011 at 3:44 PM, Tim Chase <vim@...> wrote:
          On 08/31/11 07:23, Lars Iselid wrote:
          If you have a search statement something like this:

          /[0-9]

          And want to write :w the matching content to a file. How to do? I've
          seen examples similar to this:

          /[0-9] . w>>  my.txt

          Depends on whether you want to write just the match, or the entire line.  If you want the entire line containing the match(es), it's pretty easy:

           g/[0-9]/w>> my.txt

          If you just want to write those digits to the file, it's more complex.  Something like:

           g/[0-9]/t.|s/[^0-9]\+//g|w! >> my.txt | d

          which copies each matching line to the line below, deletes all the non-digits, writes/appends the temp-line to the file, and then deletes the temp-line.

          -tim



          --
          You received this message from the "vim_use" maillist.
          Do not top-post! Type your reply below the text you are replying to.
          For more information, visit http://www.vim.org/maillist.php
        • Marcin Szamotulski
          ... (it will copy all the lines which starts with more than one digit). I think you had a small typo in your pattern. The meaning of ^ inside [] is to not
          Message 4 of 11 , Sep 1, 2011
          • 0 Attachment
            On 08:10 Thu 01 Sep , Lars Iselid wrote:
            > Thanks for help!
            >
            > I want to write the digits and actually my full regex in vim is:
            > /[-0-9]\{13,17\}
            >
            > I want numbers with 13 to 17 digits even if they have hyphens between the
            > digits. Should then be something like:
            >
            > :g/[-0-9]\{13,17\}/t.|s/[^0-9]\+//g|w! >> my.txt | d
            You can try this:
            :g/[-0-9]\{13,17\}/t.|s/^[0-9]\+//g|w! >> my.txt | d
            (it will copy all the lines which starts with more than one digit).
            I think you had a small typo in your pattern. The meaning of ^ inside [] is to
            not match any of the following characters, that's why your pattern matched the
            complement of what you expect.

            Best,
            Marcin
            >
            > I tried this but I get the whole text not just numerical digits (with
            > hyphens) in a new file.
            >
            > Lars
            >
            >
            >
            >
            > On Wed, Aug 31, 2011 at 3:44 PM, Tim Chase <vim@...> wrote:
            >
            > > On 08/31/11 07:23, Lars Iselid wrote:
            > >
            > >> If you have a search statement something like this:
            > >>
            > >> /[0-9]
            > >>
            > >> And want to write :w the matching content to a file. How to do? I've
            > >> seen examples similar to this:
            > >>
            > >> /[0-9] . w>> my.txt
            > >>
            > >
            > > Depends on whether you want to write just the match, or the entire line.
            > > If you want the entire line containing the match(es), it's pretty easy:
            > >
            > > g/[0-9]/w>> my.txt
            > >
            > > If you just want to write those digits to the file, it's more complex.
            > > Something like:
            > >
            > > g/[0-9]/t.|s/[^0-9]\+//g|w! >> my.txt | d
            > >
            > > which copies each matching line to the line below, deletes all the
            > > non-digits, writes/appends the temp-line to the file, and then deletes the
            > > temp-line.
            > >
            > > -tim
            > >
            > >
            > >
            >
            > --
            > You received this message from the "vim_use" maillist.
            > Do not top-post! Type your reply below the text you are replying to.
            > For more information, visit http://www.vim.org/maillist.php

            --
            You received this message from the "vim_use" maillist.
            Do not top-post! Type your reply below the text you are replying to.
            For more information, visit http://www.vim.org/maillist.php
          • Lars Iselid
            ... I still not just get the 13 to 17 digits strings. You say it will copy all the lines that starts with digits. I don t want the whole line just the digit
            Message 5 of 11 , Sep 1, 2011
            • 0 Attachment
              Ok, I've changed to:
              :g/[-0-9]\{13,17\}/t.|s/^[0-9]\+//g|w! >> my.txt | d

              I still not just get the 13 to 17 digits strings. You say it will copy all the lines that starts with digits. I don't want the whole line just the digit string, not what follows with exception if it's another 13 to 17 digits string.

              Lars Iselid




              On Thu, Sep 1, 2011 at 11:40 AM, Marcin Szamotulski <mszamot@...> wrote:
              On 08:10 Thu 01 Sep     , Lars Iselid wrote:
              > Thanks for help!
              >
              > I want to write the digits and actually my full regex in vim is:
              > /[-0-9]\{13,17\}
              >
              > I want numbers with 13 to 17 digits even if they have hyphens between the
              > digits. Should then be something like:
              >
              > :g/[-0-9]\{13,17\}/t.|s/[^0-9]\+//g|w! >> my.txt | d
              You can try this:
              :g/[-0-9]\{13,17\}/t.|s/^[0-9]\+//g|w! >> my.txt | d
              (it will copy all the lines which starts with more than one digit).
              I think you had a small typo in your pattern. The meaning of ^ inside [] is to
              not match any of the following characters, that's why your pattern matched the
              complement of what you expect.

              Best,
              Marcin
              >
              > I tried this but I get the whole text not just numerical digits (with
              > hyphens) in a new file.
              >
              > Lars
              >
              >
              >
              >
              > On Wed, Aug 31, 2011 at 3:44 PM, Tim Chase <vim@...> wrote:
              >
              > > On 08/31/11 07:23, Lars Iselid wrote:
              > >
              > >> If you have a search statement something like this:
              > >>
              > >> /[0-9]
              > >>
              > >> And want to write :w the matching content to a file. How to do? I've
              > >> seen examples similar to this:
              > >>
              > >> /[0-9] . w>>  my.txt
              > >>
              > >
              > > Depends on whether you want to write just the match, or the entire line.
              > >  If you want the entire line containing the match(es), it's pretty easy:
              > >
              > >  g/[0-9]/w>> my.txt
              > >
              > > If you just want to write those digits to the file, it's more complex.
              > >  Something like:
              > >
              > >  g/[0-9]/t.|s/[^0-9]\+//g|w! >> my.txt | d
              > >
              > > which copies each matching line to the line below, deletes all the
              > > non-digits, writes/appends the temp-line to the file, and then deletes the
              > > temp-line.
              > >
              > > -tim
              > >
              > >
              > >
              >
              > --
              > You received this message from the "vim_use" maillist.
              > Do not top-post! Type your reply below the text you are replying to.
              > For more information, visit http://www.vim.org/maillist.php

              --
              You received this message from the "vim_use" maillist.
              Do not top-post! Type your reply below the text you are replying to.
              For more information, visit http://www.vim.org/maillist.php

              --
              You received this message from the "vim_use" maillist.
              Do not top-post! Type your reply below the text you are replying to.
              For more information, visit http://www.vim.org/maillist.php
            • Christian Brabandt
              Hi Lars! ... I think it was correct to have the negation in the s/ command, because you want to delete (replace) all characters, but digits. Anyway, :w will
              Message 6 of 11 , Sep 1, 2011
              • 0 Attachment
                Hi Lars!

                On Do, 01 Sep 2011, Lars Iselid wrote:

                > Ok, I've changed to:
                > :g/[-0-9]\{13,17\}/t.|s/^[0-9]\+//g|w! >> my.txt | d
                >
                > I still not just get the 13 to 17 digits strings. You say it will copy all
                > the lines that starts with digits. I don't want the whole line just the
                > digit string, not what follows with exception if it's another 13 to 17
                > digits string.

                I think it was correct to have the negation in the s/ command, because
                you want to delete (replace) all characters, but digits. Anyway, :w will
                always write the whole buffer, if not given a range, so try changing the
                command to

                :g/[-0-9]\{13,17\}/t.|s/[^0-9]\+//g|.w! >> my.txt | d

                regards,
                Christian

                --
                You received this message from the "vim_use" maillist.
                Do not top-post! Type your reply below the text you are replying to.
                For more information, visit http://www.vim.org/maillist.php
              • Lars Iselid
                Still gets the whole content to the my.txt file. How to not write the whole buffer? Lars Iselid ... -- You received this message from the vim_use maillist.
                Message 7 of 11 , Sep 1, 2011
                • 0 Attachment
                  Still gets the whole content to the my.txt file. How to not write the whole buffer?

                  Lars Iselid




                  On Thu, Sep 1, 2011 at 12:24 PM, Christian Brabandt <cblists@...> wrote:
                  Hi Lars!

                  On Do, 01 Sep 2011, Lars Iselid wrote:

                  > Ok, I've changed to:
                  > :g/[-0-9]\{13,17\}/t.|s/^[0-9]\+//g|w! >> my.txt | d
                  >
                  > I still not just get the 13 to 17 digits strings. You say it will copy all
                  > the lines that starts with digits. I don't want the whole line just the
                  > digit string, not what follows with exception if it's another 13 to 17
                  > digits string.

                  I think it was correct to have the negation in the s/ command, because
                  you want to delete (replace) all characters, but digits. Anyway, :w will
                  always write the whole buffer, if not given a range, so try changing the
                  command to

                  :g/[-0-9]\{13,17\}/t.|s/[^0-9]\+//g|.w! >> my.txt | d

                  regards,
                  Christian

                  --
                  You received this message from the "vim_use" maillist.
                  Do not top-post! Type your reply below the text you are replying to.
                  For more information, visit http://www.vim.org/maillist.php

                  --
                  You received this message from the "vim_use" maillist.
                  Do not top-post! Type your reply below the text you are replying to.
                  For more information, visit http://www.vim.org/maillist.php
                • Christian Brabandt
                  Hi Lars! ... Please show the exact command you used. regards, Christian -- -- You received this message from the vim_use maillist. Do not top-post! Type your
                  Message 8 of 11 , Sep 1, 2011
                  • 0 Attachment
                    Hi Lars!

                    On Do, 01 Sep 2011, Lars Iselid wrote:

                    > Still gets the whole content to the my.txt file. How to not write the whole
                    > buffer?

                    Please show the exact command you used.

                    regards,
                    Christian
                    --

                    --
                    You received this message from the "vim_use" maillist.
                    Do not top-post! Type your reply below the text you are replying to.
                    For more information, visit http://www.vim.org/maillist.php
                  • Lars Iselid
                    Checked carefully again and I had missed a point. Sorry! But in my result each string of digits (10 to 17) from same line (or paragraph) is sorted in one
                    Message 9 of 11 , Sep 1, 2011
                    • 0 Attachment
                      Checked carefully again and I had missed a point. Sorry! But in my result each string of digits (10 to 17) from same line (or paragraph) is sorted in one string without spaces. How to get the output sorted in one column with each string of digits in each row? Or at least spaces between each string of digits?

                      Lars




                      On Thu, Sep 1, 2011 at 1:06 PM, Christian Brabandt <cblists@...> wrote:
                      Hi Lars!

                      On Do, 01 Sep 2011, Lars Iselid wrote:

                      > Still gets the whole content to the my.txt file. How to not write the whole
                      > buffer?

                      Please show the exact command you used.

                      regards,
                      Christian
                      --

                      --
                      You received this message from the "vim_use" maillist.
                      Do not top-post! Type your reply below the text you are replying to.
                      For more information, visit http://www.vim.org/maillist.php

                      --
                      You received this message from the "vim_use" maillist.
                      Do not top-post! Type your reply below the text you are replying to.
                      For more information, visit http://www.vim.org/maillist.php
                    • Christian Brabandt
                      Hi Lars! ... Can you please share an example, what your data looks like before and what it looks like afterwards and how it should differ. I find it hard to
                      Message 10 of 11 , Sep 1, 2011
                      • 0 Attachment
                        Hi Lars!

                        On Do, 01 Sep 2011, Lars Iselid wrote:

                        > Checked carefully again and I had missed a point. Sorry! But in my result
                        > each string of digits (10 to 17) from same line (or paragraph) is sorted in
                        > one string without spaces. How to get the output sorted in one column with
                        > each string of digits in each row? Or at least spaces between each string of
                        > digits?

                        Can you please share an example, what your data looks like before and
                        what it looks like afterwards and how it should differ. I find it hard
                        to understand what you want.

                        regards,
                        Christian

                        --
                        You received this message from the "vim_use" maillist.
                        Do not top-post! Type your reply below the text you are replying to.
                        For more information, visit http://www.vim.org/maillist.php
                      • Tim Chase
                        ... For this, I d tend to do something like put each match on its own line and then delete all lines that don t contain that pattern. Something like ... Note
                        Message 11 of 11 , Sep 1, 2011
                        • 0 Attachment
                          On 09/01/11 01:10, Lars Iselid wrote:
                          > I want to write the digits and actually my full regex in vim
                          > is: /[-0-9]\{13,17\}
                          >
                          > I want numbers with 13 to 17 digits even if they have hyphens
                          > between the digits.

                          For this, I'd tend to do something like put each match on its own
                          line and then delete all lines that don't contain that pattern.
                          Something like

                          :%s/[-0-9]\{13,17\}/\r&\r/g
                          :v/[-0-9]\{13,17\}/d

                          Note that this does modify your current buffer, so either (1)
                          operate on a scratch buffer or (2) remember to undo it before
                          saving if your original source data isn't easily reproducible.

                          It gets a little trickier if you have multiple matches on the
                          same line AND you want to keep them on the same line (the above
                          puts each match in the same line onto its own line).

                          -tim


                          --
                          You received this message from the "vim_use" maillist.
                          Do not top-post! Type your reply below the text you are replying to.
                          For more information, visit http://www.vim.org/maillist.php
                        Your message has been successfully submitted and would be delivered to recipients shortly.