View Full Version : Regexps in emacs / sed - delete every other line of a file?
Zorgoth
July 18th, 2010, 01:01 PM
Exactly as the title suggests, I am trying to find a way to delete every other line of a file using either emacs or sed. If you know how to do it in both emacs and sed I would appreciate it if you could show me both ways so I can understand regexps better.
xsinsx
July 18th, 2010, 05:17 PM
simple enough.
Macintosh:Desktop stephenjackson$ cat text.txt
1
2
3
4
5
6
7
8
9
Macintosh:Desktop stephenjackson$ sed -n 'p;N' text.txt
1
3
5
7
9
DaithiF
July 18th, 2010, 06:44 PM
Hi,
also, in gnu sed you can do:
sed '2~2d' testfile
where the pattern is firstline~step, ie. starting from line 2 delete every 2nd line
Zorgoth
July 18th, 2010, 11:59 PM
I know little about sed so this may not be very elegant but to delete three of every four lines in a file
sed '1~4p;1~1d' /path/to/file > /path/to/modified/file
should work
Zorgoth
July 18th, 2010, 11:59 PM
oops wrong thread
jpkotta
July 19th, 2010, 02:34 AM
You don't need regexps to do what you want. (This is not tested very well.)
(defun delete-even-lines ()
(interactive)
(save-excursion
(beginning-of-buffer)
(while (not (eobp))
(forward-line 1)
(when (not (eobp))
(delete-region (line-beginning-position)
(1+ (line-end-position)))))))
Powered by vBulletin® Version 4.2.2 Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.