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

Re: Vim Help for deleting alternate lines in text

Expand Messages
  • Auro Ashish Saha
    Hello Antoine, Thanks for your post. I tried both the options but i could not get any result. I am using gvim. Please help. Regards, Auro Ashish Saha. ... --
    Message 1 of 7 , Mar 30, 2007
      Hello Antoine,

      Thanks for your post. I tried both the options but i could not get any
      result. I am using gvim. Please help.

      Regards,

      Auro Ashish Saha.
      > Auro Ashish Saha wrote:
      >> Hello,
      >>
      >> Please help me to remove alternate lines from a text file.
      >>
      >> 000000 00000
      >> 123456 99999
      >> 999999 99999
      >> 123445 99999
      >>
      >> I want to delete the line 1, 3, 5 and so on. What are the commands to be
      >> used. Thanks for help in advance.
      >>
      >> Regards,
      >>
      >> Auro Ashish Saha.
      >>
      >>
      >>
      >
      > Method I:
      >
      > q"ddjjq
      > <count>@"
      >
      > where <count> is equal to the number of lines still to be deleted
      >
      > Method II (all on one line if typed on the Vim command-line):
      >
      > :let i=1 | while i <= line('$') | if (i % 2) | exe i . "delete" |
      > endif |
      > endwhile
      >
      >
      > Best regards,
      > Tony.
      > --
      > Idaho state law makes it illegal for a man to give his sweetheart a box
      > of candy weighing less than fifty pounds.
      >


      --
      Auro Ashish Saha
      QIP Ph.D. Research Scholar
      Suman Mashruwala Laboratory
      Mechanical Engineering Department
      IIT Bombay, Powai
      Mumbai - 400 076
      India


      Mobile: 09969074835

      Email: asaha@...
      saa_1973@...
    • Gary Johnson
      ... One problem with this is that it attempts to store a sequence of normal-mode commands into the register, which is the unnamed register, but then deletes
      Message 2 of 7 , Mar 30, 2007
        On 2007-03-31, Auro Ashish Saha <asaha@...> wrote:
        > Hello Antoine,
        >
        > Thanks for your post. I tried both the options but i could not get any
        > result. I am using gvim. Please help.
        >
        > Regards,
        >
        > Auro Ashish Saha.
        > > Auro Ashish Saha wrote:
        > >> Hello,
        > >>
        > >> Please help me to remove alternate lines from a text file.
        > >>
        > >> 000000 00000
        > >> 123456 99999
        > >> 999999 99999
        > >> 123445 99999
        > >>
        > >> I want to delete the line 1, 3, 5 and so on. What are the commands to be
        > >> used. Thanks for help in advance.
        > >>
        > >> Regards,
        > >>
        > >> Auro Ashish Saha.
        > >>
        > >>
        > >>
        > >
        > > Method I:
        > >
        > > q"ddjjq
        > > <count>@"
        > >
        > > where <count> is equal to the number of lines still to be deleted

        One problem with this is that it attempts to store a sequence of
        normal-mode commands into the " register, which is the unnamed
        register, but then deletes the first line into this same register.

        The other problem with it is that it moves the cursor too many
        times: once the first line has been deleted with dd, the cursor
        automatically moves to the next line, so only one j is needed to get
        to the next line to be deleted.

        This version will work:

        qqddjq
        <count>@q

        Note that it uses the q register instead of the " register. Also,
        if you don't want to try to figure out what <count> should be, and
        if you don't want to remove a huge number of lines, you can execute
        the recorded commands the first time after you record them with this
        command:

        @q

        and every subsequent time with this command:

        @@

        That way, once you've recorded the command and executed it once from
        the q register, you can just hold your finger on the @ key and watch
        the lines disappear. As you get close to the bottom of your file,
        you can start slowing down and typing just two @'s at a time.

        > >
        > > Method II (all on one line if typed on the Vim command-line):
        > >
        > > :let i=1 | while i <= line('$') | if (i % 2) | exe i . "delete" |
        > > endif |
        > > endwhile

        It looks as though Tony left out part of Method II: i is never
        incremented. I modified it as shown below (added "let i += 1 |")and
        verified that it works.

        :let i=1 | while i <= line('$') | if (i % 2) | exe i . "delete" | endif | let i += 1 | endwhile

        HTH,
        Gary

        --
        Gary Johnson | Agilent Technologies
        garyjohn@... | Mobile Broadband Division
        | Spokane, Washington, USA
      • Andy Wokula
        ... Macros stop if an error (- beep) occurs 999@q will stop if end-of-file reached because j cannot go further. ... -- Regards, Andy EOM
        Message 3 of 7 , Mar 31, 2007
          Gary Johnson schrieb:
          >>> Auro Ashish Saha wrote:
          >>>> Hello,
          >>>>
          >>>> Please help me to remove alternate lines from a text file.
          >>>>
          >>>> 000000 00000
          >>>> 123456 99999
          >>>> 999999 99999
          >>>> 123445 99999
          >>>>
          >>>> I want to delete the line 1, 3, 5 and so on. What are the commands to be
          >>>> used. Thanks for help in advance.
          >>>>
          >>> Method I:
          >>>
          >>> q"ddjjq
          >>> <count>@"
          >>>
          >>> where <count> is equal to the number of lines still to be deleted
          >
          > This version will work:
          >
          > qqddjq
          > <count>@q
          >
          > Note that it uses the q register instead of the " register. Also,
          > if you don't want to try to figure out what <count> should be, and
          > if you don't want to remove a huge number of lines, you can execute
          > the recorded commands the first time after you record them with this
          > command:
          >
          > @q
          >
          > and every subsequent time with this command:
          >
          > @@
          >
          > That way, once you've recorded the command and executed it once from
          > the q register, you can just hold your finger on the @ key and watch
          > the lines disappear. As you get close to the bottom of your file,
          > you can start slowing down and typing just two @'s at a time.

          Macros stop if an error (-> beep) occurs

          999@q

          will stop if end-of-file reached because "j" cannot go further.

          >>> Method II (all on one line if typed on the Vim command-line):
          >>>
          >>> :let i=1 | while i <= line('$') | if (i % 2) | exe i . "delete" |
          >>> endif |
          >>> endwhile
          >
          > It looks as though Tony left out part of Method II: i is never
          > incremented. I modified it as shown below (added "let i += 1 |")and
          > verified that it works.
          >
          > :let i=1 | while i <= line('$') | if (i % 2) | exe i . "delete" | endif | let i += 1 | endwhile
          >
          > HTH,
          > Gary

          Method III:

          :2,$-1g/^/+1d
          :1d

          --
          Regards,
          Andy

          EOM
        • A.J.Mechelynck
          ... Duh. My bad. Apparently I was lacking sleep when I typed that email. Best regards, Tony. -- The primary purpose of the DATA statement is to give names to
          Message 4 of 7 , Mar 31, 2007
            Gary Johnson wrote:
            > On 2007-03-31, Auro Ashish Saha <asaha@...> wrote:
            >> Hello Antoine,
            >>
            >> Thanks for your post. I tried both the options but i could not get any
            >> result. I am using gvim. Please help.
            >>
            >> Regards,
            >>
            >> Auro Ashish Saha.
            >>> Auro Ashish Saha wrote:
            >>>> Hello,
            >>>>
            >>>> Please help me to remove alternate lines from a text file.
            >>>>
            >>>> 000000 00000
            >>>> 123456 99999
            >>>> 999999 99999
            >>>> 123445 99999
            >>>>
            >>>> I want to delete the line 1, 3, 5 and so on. What are the commands to be
            >>>> used. Thanks for help in advance.
            >>>>
            >>>> Regards,
            >>>>
            >>>> Auro Ashish Saha.
            >>>>
            >>>>
            >>>>
            >>> Method I:
            >>>
            >>> q"ddjjq
            >>> <count>@"
            >>>
            >>> where <count> is equal to the number of lines still to be deleted
            >
            > One problem with this is that it attempts to store a sequence of
            > normal-mode commands into the " register, which is the unnamed
            > register, but then deletes the first line into this same register.
            >
            > The other problem with it is that it moves the cursor too many
            > times: once the first line has been deleted with dd, the cursor
            > automatically moves to the next line, so only one j is needed to get
            > to the next line to be deleted.
            >
            > This version will work:
            >
            > qqddjq
            > <count>@q
            >
            > Note that it uses the q register instead of the " register. Also,
            > if you don't want to try to figure out what <count> should be, and
            > if you don't want to remove a huge number of lines, you can execute
            > the recorded commands the first time after you record them with this
            > command:
            >
            > @q
            >
            > and every subsequent time with this command:
            >
            > @@
            >
            > That way, once you've recorded the command and executed it once from
            > the q register, you can just hold your finger on the @ key and watch
            > the lines disappear. As you get close to the bottom of your file,
            > you can start slowing down and typing just two @'s at a time.
            >
            >>> Method II (all on one line if typed on the Vim command-line):
            >>>
            >>> :let i=1 | while i <= line('$') | if (i % 2) | exe i . "delete" |
            >>> endif |
            >>> endwhile
            >
            > It looks as though Tony left out part of Method II: i is never
            > incremented. I modified it as shown below (added "let i += 1 |")and
            > verified that it works.
            >
            > :let i=1 | while i <= line('$') | if (i % 2) | exe i . "delete" | endif | let i += 1 | endwhile
            >
            > HTH,
            > Gary
            >

            Duh. My bad. Apparently I was lacking sleep when I typed that email.

            Best regards,
            Tony.
            --
            The primary purpose of the DATA statement is to give names to
            constants; instead of referring to pi as 3.141592653589793 at every
            appearance, the variable PI can be given that value with a DATA
            statement and used instead of the longer form of the constant. This
            also simplifies modifying the program, should the value of pi change.
            -- FORTRAN manual for Xerox Computers
          • Alan G Isaac
            ... But did you try ... as suggested at http://rayninfo.co.uk/vimtips.html ?? Cheers, Alan Isaac
            Message 5 of 7 , Mar 31, 2007
              On Sat, 31 Mar 2007, (IST) Auro Ashish Saha apparently wrote:
              > i could not get any result

              But did you try
              :%norm jdd
              as suggested at
              http://rayninfo.co.uk/vimtips.html ??

              Cheers,
              Alan Isaac
            Your message has been successfully submitted and would be delivered to recipients shortly.