[comp.emacs] A regexp for blank lines

jthomas@nmsu.CSNET (03/09/88)

(I'm using gnu emacs 18.44.22.)

I am trying to write elisp code to remove lines from a buffer.  My intent
was to remove any lines containing a match for a regular expression, as in:
    (let ((zap-pattern (read-string "Pattern for lines to remove: ")))
      (goto-char (point-min))
      (while (re-search-forward zap-pattern nil t)
	(forward-line 0)
	(kill-line 1)))
.  I thought I might try to let this eliminate blank lines by trying "^$"
for the pattern.  But that infinite looped at the end of the buffer (I
guess that's reasonable?).  "\C-j$" has the same problem.  "^\C-j" doesn't
loop, but it kills the wrong line given the above code.

Is there a way?

Jim Thomas      jthomas@nmsu.csnet

jk3k+@ANDREW.CMU.EDU (Joe Keane) (03/10/88)

How about the function delete-matching-lines?  Or since that works only on 
lines after point, you might want:
(defun delete-all-matching-lines regexp
  (interactive "sFlush all lines matching regexp: ")
  (save-excursion
    (goto-char (point-min))
    (delete-matching-lines regexp)))

--Joe

arc1@eagle.ukc.ac.uk (A.R.Curtis) (03/10/88)

Expires:

Sender:

Followup-To:


In article <8803091028.AA17077@ucbvax.Berkeley.EDU> jthomas@nmsu.CSNET writes:
>
>I am trying to write elisp code to remove lines from a buffer.  My intent
>...
>.  I thought I might try to let this eliminate blank lines by trying "^$"


Well, there are at least two alternatives.  If you want to stick with
the idea of using regexps then there is a function called
delete-matching-lines which is probably what you want. 

A specific way of doing what you want with blank lines is to remove all
occurrences of the string ^J^J (i.e.  a blank line consists of the
string newline-newline)


Tony



-- 
Tony Curtis, Computing Lab.                | arc1@uk.ac.ukc
Univ. Kent at Canterbury                   | 
Canterbury, Kent CT2 7NF                   | tcu@uk.ac.ex.cs

jr@PEBBLES.BBN.COM (John Robinson) (03/14/88)

>> A specific way of doing what you want with blank lines is to remove all
>> occurrences of the string ^J^J (i.e.  a blank line consists of the
>> string newline-newline)

Ahem ... removing them would glue the surrounding non-blank lines
together.  You have to replace occurences of ^J^J with ^J.  If there
are stretches of more than one blank line, you may have to do this
more than once on the buffer.

/jr
jr@bbn.com or jr@bbn.uucp

sbb@smpvax1.UUCP (03/15/88)

   Reply-To: trwrb!ucbvax!BBN.COM!jr
   Date: Sun, 13 Mar 88 22:53:43 -0500
   From: John Robinson <trwrb!ucbvax!PEBBLES.BBN.COM!jr>

   ....

   Ahem ... removing them would glue the surrounding non-blank lines
   together.  You have to replace occurences of ^J^J with ^J.  If there
   are stretches of more than one blank line, you may have to do this
   more than once on the buffer.

   /jr
   jr@bbn.com or jr@bbn.uucp


Well, couldn't you use regexp of the form "\^J\^J+"?  This would
eliminate the need for multiple passes.

steve
-----------------------
Steve Byrne				...ucbvax!trwrb!smpvax1!sbb
Inference Corp.				(213) 417-7997
5300 W. Century Blvd.
Los Angeles, CA  90045

davidsen@steinmetz.steinmetz.UUCP (William E. Davidsen Jr) (03/16/88)

sed '/^$/d' infile > outfile

also doesn't remove lines which are not null, which you can do by
	sed '/^[ \t]*$/d' infile > outfile
-- 
	bill davidsen		(wedu@ge-crd.arpa)
  {uunet | philabs | seismo}!steinmetz!crdos1!davidsen
"Stupidity, like virtue, is its own reward" -me