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

Re: delete Numbera of lines every Numberb of lines

Expand Messages
  • Gareth Oakes
    qa9ddjq@a@a@a@a@a...etc...
    Message 1 of 7 , Dec 1, 2005
    • 0 Attachment
      <esc>qa9ddjq@a@a@a@a@a...etc...

      Vim Visual wrote:
      > Hi,
      >
      > do you know how to delete, say, nine lines every ten lines in a data file?
      >
      > For instance, delete all lines betwenn the line with xxxxx and eeeeee
      > here below... And then again and again (the file has over 6000 lines)
      >
      > xxxxxxxxxxxxxxxxxxxxxxxxx
      > aaaaaaaaaaaaaaaaaaaaaaaaa
      > ddddddddddddddddddddddddd
      > fffffffffffffffffffffffffffffffffffffffffff
      > gggggggggggggggggggggggg
      > hhhhhhhhhhhhhhhhhhhhhhhh
      > jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj
      > kkkkkkkkkkkkkkkkkkkkkkkkk
      > lllllllllllllllllllllllllllllllllllllllllllllllllllllllll
      > qqqqqqqqqqqqqqqqqqqqqqqqq
      > wwwwwwwwwwwwwwwwwww
      > eeeeeeeeeeeeeeeeeeeeeeeee
      >
      > thanks
      >
    • Andrej Mikus
      ... This appears a task for batch processing, not editor. like perl -ne print unless $. % 10 ... perl -ne print unless /xxxxx/ .. /eeeeee/ this deletes
      Message 2 of 7 , Dec 1, 2005
      • 0 Attachment
        On Thu, 01.Dec.05 12:48:12 +0100, Vim Visual wrote:
        > Hi,
        >
        > do you know how to delete, say, nine lines every ten lines in a data file?

        This appears a task for batch processing, not editor. like

        perl -ne 'print unless $. % 10'


        > For instance, delete all lines betwenn the line with xxxxx and eeeeee
        > here below... And then again and again (the file has over 6000 lines)

        perl -ne 'print unless /xxxxx/ .. /eeeeee/'

        this deletes also the lines with xxxxx and eeeeee.

        Andrej
      • Tim Chase
        ... If you re looking for a pure Vim solution (rather than Andrej s Perl solution), you may have to tag each line, then delete the ... One non-solution that
        Message 3 of 7 , Dec 1, 2005
        • 0 Attachment
          > do you know how to delete, say, nine lines every ten lines in a data file?

          If you're looking for a pure Vim solution (rather than Andrej's
          Perl solution), you may have to tag each line, then delete the
          tagged lines...something like this three-step:

          :%s/^/\=line(".")%10==0
          :g/^0/d
          :%s/^1

          One non-solution that seemed like it might have been able to work is

          :g/^/if (line(".")%10!=0) | d | endif

          Unfortunately, by deleting line #1, it makes line #2 become line
          #1 which, when it's checked, is now candidate for
          deletion...thus, you end up with an empty file :(

          If you want to do it in one pass in Vim, this little ugly bugger
          should also do the trick:

          let delcount=0 | g/^/if ((delcount+line("."))%10!=0) | d | let
          delcount=delcount + 1| endif

          (all on one line...it might be over-parenthesized, but better
          safe than sorry when it comes to default order of operations :)

          If you don't have Perl, but do have sed, you can filter through

          :%!sed -n '10,${p;n;n;n;n;n;n;n;n;n}'

          or with awk

          :%!awk '!(NR%10) {print $0}'

          Just so as you have plenty of options :)

          -tim
        • Georg Lohrer
          ... The recording-feature of vim is one of my absolute favourites. It has saved me hours of time while changing and reformatting bunches of source-code with
          Message 4 of 7 , Dec 1, 2005
          • 0 Attachment
            Am Donnerstag, 1. Dezember 2005 13:11 schrieb Gareth Oakes:
            > <esc>qa9ddjq@a@a@a@a@a...etc...
            >

            The recording-feature of vim is one of my absolute favourites. It has saved me
            hours of time while changing and reformatting bunches of source-code with
            rather tedious actions.

            Ciao, Georg
          • Antony Scriven
            ... Antony
            Message 5 of 7 , Dec 1, 2005
            • 0 Attachment
              On Dec 01, Tim Chase wrote:

              > [...]
              >
              > let delcount=0 | g/^/if ((delcount+line("."))%10!=0) | d | let
              > delcount=delcount + 1| endif

              You could shorten that somewhat, if you wanted:

              :let i=0 | g/^/ let i=i+1 | if i%10 | d

              Antony
            • Preben 'Peppe' Guldberg
              ... Assuming you want to keep the first and every tenth line in the buffer, you could use eg. ... Here nine lines are deleted and the tenth is moved after
              Message 6 of 7 , Dec 1, 2005
              • 0 Attachment
                Vim Visual wrote:

                > do you know how to delete, say, nine lines every ten lines in a data file?

                Assuming you want to keep the first and every tenth line in the buffer,
                you could use eg.

                :g/^/+d9

                If you want to keep every tenth line, you need a little magic:

                :g/^/d9|m.

                Here nine lines are deleted and the tenth is moved after itself.
                This little trick removes the tenth line from the list of lines that
                :global processes (as does the deletion of line 2 to 9).

                Peppe
                --
                se nocp cpo=BceFsx!$ hid bs=2 ls=2 hls ic " P. Guldberg /bin/vi@...
                se scs ai isf-== fdo-=block cino=t0,:0 hi=100 ru so=4 noea lz|if has('unix')
                se sh=/bin/sh|en|syn on|filetype plugin indent on|ono S V/\n^-- $\\|\%$/<CR>
                cno <C-A> <C-B>|au FileType vim,mail se sw=4 sts=4 et|let&tw=72+6*(&ft=~'v')
              Your message has been successfully submitted and would be delivered to recipients shortly.