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
[EMAIL PROTECTED]
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