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

how to delete lines containing a pattern and not containing another pattern?

Expand Messages
  • 김찬
    I’m a long-time vi(m) user and I have question. Can I remove lines that contain a pattern but not another pattern? For example, Line 1 : rabbit Line 2 : arch
    Message 1 of 7 , Jan 23, 2014
    • 0 Attachment

       

      I’m a long-time vi(m) user and I have question.

      Can I remove lines that contain a pattern but not another pattern?

      For example,

       

      Line 1 : rabbit

      Line 2 : arch sparc

      Line 3 : arch arm

      Line 4 : tiger

      I want to remove lines containing arch but not containing sparc.

      So Here I want to remove only line 3 leaving lines 1, 3 and 4 intact.

       

      I can remove lines containg ‘arch’ by

      :g/arch/d

      I can remove lines not containing ‘sparc’ by

      :g!/sparc/d

      How do I combine those two?

      I tried

      :g/arch/&!/sparc/d  or :g/arch&!sparc/d

      But of course it doesn’t work. (with escape before ! also)

      Is there a way to remove only line 3? I looked it up in the manual but in vain..

      Thank you!

      -------------------------------------------------------------------------

      김찬, 책임연구원

      한국전자통신연구원 융합기술연구부문 멀티미디어프로세서연구실

      Chan Kim, Principal Researcher

      ETRI (Electronics and Telecommnications Research Institute)

      O)+82-42-860-5773  C) +82-10-3418-9152

      E)ckim@...       web) http://chankim.dothome.co.kr

       
       

      --
      --
      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 because you are subscribed to the Google Groups "vim_use" group.
      To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@....
      For more options, visit https://groups.google.com/groups/opt_out.
    • Tim Chase
      ... Dr. Chip s LogiPat plugin[1] makes quick work of generating such patterns. I did ... and it gives me the pattern %(.*arch.* &^ %( %(sparc ) @!. )*$ ) ...
      Message 2 of 7 , Jan 23, 2014
      • 0 Attachment
        On 2014-01-23 14:51, 김찬 wrote:
        > I’m a long-time vi(m) user and I have question.
        > Can I remove lines that contain a pattern but not another pattern?
        > For example,
        >
        > Line 1 : rabbit
        > Line 2 : arch sparc
        > Line 3 : arch arm
        > Line 4 : tiger
        > I want to remove lines containing arch but not containing sparc.
        > So Here I want to remove only line 3 leaving lines 1, 3 and 4
        > intact.

        Dr. Chip's LogiPat plugin[1] makes quick work of generating such
        patterns. I did

        :echo LogiPat('"arch"&!"sparc"')

        and it gives me the pattern

        \%(.*arch.*\&^\%(\%(sparc\)\@!.\)*$\)

        which I can then use in a :g/pattern/d command:

        :g/\%(.*arch.*\&^\%(\%(sparc\)\@!.\)*$\)/d



        [1] http://www.vim.org/scripts/script.php?script_id=1290

        --
        --
        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 because you are subscribed to the Google Groups "vim_use" group.
        To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@....
        For more options, visit https://groups.google.com/groups/opt_out.
      • Chan Kim
        ... Hi Tim, Wow thank you. without the plugin it looks too complex.. Maybe vim developers should consider adding pattern combination function with simpler
        Message 3 of 7 , Jan 23, 2014
        • 0 Attachment
          On Friday, January 24, 2014 1:32:53 AM UTC+9, Tim Chase wrote:
          > On 2014-01-23 14:51, 김찬 wrote: > I’m a long-time vi(m) user and I have question. > Can I remove lines that contain a pattern but not another pattern? > For example, > > Line 1 : rabbit > Line 2 : arch sparc > Line 3 : arch arm > Line 4 : tiger > I want to remove lines containing arch but not containing sparc. > So Here I want to remove only line 3 leaving lines 1, 3 and 4 > intact. Dr. Chip's LogiPat plugin[1] makes quick work of generating such patterns. I did :echo LogiPat('"arch"&!"sparc"') and it gives me the pattern \%(.*arch.*\&^\%(\%(sparc\)\@!.\)*$\) which I can then use in a :g/pattern/d command: :g/\%(.*arch.*\&^\%(\%(sparc\)\@!.\)*$\)/d [1] http://www.vim.org/scripts/script.php?script_id=1290

          Hi Tim,
          Wow thank you. without the plugin it looks too complex..
          Maybe vim developers should consider adding pattern combination function with simpler syntax..
          Chan

          --
          --
          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 because you are subscribed to the Google Groups "vim_use" group.
          To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@....
          For more options, visit https://groups.google.com/groups/opt_out.
        • Ben Fritz
          ... I d approach this as on every line containing arch, delete the line if it does ... Note I used !~ for a regular expression match which respects the
          Message 4 of 7 , Jan 23, 2014
          • 0 Attachment
            On Thursday, January 23, 2014 8:51:16 AM UTC-6, Chan Kim wrote:
            >  
            > I’m a long-time vi(m) user and I have question.
            > Can I remove lines that contain a pattern but not another pattern?
            > For example,
            >  
            > Line 1 : rabbit
            > Line 2 : arch sparc
            > Line 3 : arch arm
            > Line 4 : tiger
            > I want to remove lines containing arch but not containing sparc.
            > So Here I want to remove only line 3 leaving lines 1, 3 and 4 intact.
            >  

            I'd approach this as "on every line containing arch, delete the line if it does
            not contain sparc". This can be done as follows:

            :g#arch#if getline('.')!~'sparc' | d | endif

            Note I used !~ for a regular expression match which respects the 'ignorecase'
            setting. I think this is appropriate here since you are doing this
            interactively; if you were writing a plugin or you need strict case sensitivity
            (or insensitivity) then you should use !~# or !~?.

            --
            --
            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 because you are subscribed to the Google Groups "vim_use" group.
            To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@....
            For more options, visit https://groups.google.com/groups/opt_out.
          • Chan Kim
            ... Thank you Tim and Ben (I ll read Ben s post later..) But I found maybe the nicest one. The following line does it. ... I guessed the meaning of :v command
            Message 5 of 7 , Jan 24, 2014
            • 0 Attachment
              On Thursday, January 23, 2014 11:51:16 PM UTC+9, Chan Kim wrote:
              >  I’m a long-time vi(m) user and I have question.Can I remove lines that contain a pattern but not another pattern?For example, Line 1 : rabbitLine 2 : arch sparcLine 3 : arch armLine 4 : tigerI want to remove lines containing arch but not containing sparc.So Here I want to remove only line 3 leaving lines 1, 3 and 4 intact. I can remove lines containg ‘arch’ by:g/arch/dI can remove lines not containing ‘sparc’ by:g!/sparc/dHow do I combine those two?I tried:g/arch/&!/sparc/d  or :g/arch&!sparc/dBut of course it doesn’t work. (with escape before ! also)Is there a way to remove only line 3? I looked it up in the manual but in vain..Thank you!
              -------------
              Thank you Tim and Ben (I'll read Ben's post later..)
              But I found maybe the nicest one. The following line does it.
              :v /sparc/s/.*arch.*\n//
              I guessed the meaning of :v command concluding that it says
              'for every line not containing 'sparc', replace '.*arch.*\n'(line containing arch together with return) with null. in case of no search pattern(..arch..), ignore it.
              ref : http://stackoverflow.com/questions/11807713/multiple-g-and-v-commands-in-one-statement (Zecc's reponse)

              Chan

              --
              --
              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 because you are subscribed to the Google Groups "vim_use" group.
              To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@....
              For more options, visit https://groups.google.com/groups/opt_out.
            • Arun E
              ... Careful with line deleting substitutions when using :g/:v. It will not work always: For eg. with a file like this -- Line 2 : arch sparc asd Line 3 : arch
              Message 6 of 7 , Jan 28, 2014
              • 0 Attachment
                On Friday, January 24, 2014 4:02:05 AM UTC-8, Chan Kim wrote:
                > On Thursday, January 23, 2014 11:51:16 PM UTC+9, Chan Kim wrote:
                >
                > >  I’m a long-time vi(m) user and I have question.Can I remove lines that contain a pattern but not another pattern?For example, Line 1 : rabbitLine 2 : arch sparcLine 3 : arch armLine 4 : tigerI want to remove lines containing arch but not containing sparc.So Here I want to remove only line 3 leaving lines 1, 3 and 4 intact. I can remove lines containg ‘arch’ by:g/arch/dI can remove lines not containing ‘sparc’ by:g!/sparc/dHow do I combine those two?I tried:g/arch/&!/sparc/d  or :g/arch&!sparc/dBut of course it doesn’t work. (with escape before ! also)Is there a way to remove only line 3? I looked it up in the manual but in vain..Thank you!
                >
                > -------------
                >
                > Thank you Tim and Ben (I'll read Ben's post later..)
                >
                > But I found maybe the nicest one. The following line does it.
                >
                > :v /sparc/s/.*arch.*\n//
                >

                Careful with line deleting substitutions when using :g/:v. It will not work always:
                For eg. with a file like this
                --
                Line 2 : arch sparc asd
                Line 3 : arch arm
                Line 4 : arch aaa
                ---
                ..the "arch aaa" will not be deleted.

                Tim's & Ben's method would do it.

                One more way to do this is:
                :g/.*arch\&.*\(sparc.*\)\@<!$/d

                --Arun

                --
                --
                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 because you are subscribed to the Google Groups "vim_use" group.
                To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@....
                For more options, visit https://groups.google.com/groups/opt_out.
              • Paul
                ... Remove the n from the command and the last line is removed. However, aren t all lines, including the last, unless in binary mode, appended with a newline?
                Message 7 of 7 , Jan 28, 2014
                • 0 Attachment
                  On Tuesday, 28 January, 2014 at 09:17:56 GMT, Arun E wrote:
                  >On Friday, January 24, 2014 4:02:05 AM UTC-8, Chan Kim wrote:
                  >> :v /sparc/s/.*arch.*\n//
                  >
                  >Careful with line deleting substitutions when using :g/:v. It will not work always:
                  >For eg. with a file like this
                  >--
                  >Line 2 : arch sparc asd
                  >Line 3 : arch arm
                  >Line 4 : arch aaa
                  >---
                  >..the "arch aaa" will not be deleted.

                  Remove the \n from the command and the last line is removed. However, aren't all lines, including the last, unless in binary mode, appended with a newline? For example, write a new file such as above, cat it and you'll see a newline at the end (the shell prompt is on a new line). 'set list' shows the end of line marker ($) (as does 'cat -E'), but not specifically a newline marker. How to have vim show newlines with 'set list'? I don't see it in listchars.

                  --
                  --
                  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 because you are subscribed to the Google Groups "vim_use" group.
                  To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@....
                  For more options, visit https://groups.google.com/groups/opt_out.
                Your message has been successfully submitted and would be delivered to recipients shortly.