1

Suppose I has the next text:

test
HELLO
help
HOW ARE YOU
buy

I need to get (filter) only text lines with all uppercase chars. So result must be like this:

HELLO
HOW ARE YOU
  • Your result contains newline and space which are not uppercase chars. – xuchunyang Dec 30 '18 at 13:33
  • ok, with all uppercase, spaces and newline. How I can do this? – a_subscriber Dec 30 '18 at 15:23
  • So the question is about filtering text line by line! – Heikki Dec 31 '18 at 12:40
1

Do you want this? only-uppercase is used to test which item should be remove(t:leave, nil: remove). Then seq-filter to filter the input items.

(defun only-uppercase (str)
  "only uppercase"
  (if (numberp
       (let ((case-fold-search nil))
     (string-match-p "[a-z]" str)))
      nil t))

(defun filter-uppercase ()
  "filter uppercase"
  (interactive)
  (print
  (seq-filter
   'only-uppercase
    (split-string
    (buffer-substring-no-properties (region-beginning) (region-end))
    "\n" t))))
1

There are two very useful interactive built-in functions flush-lines and keep-lines that take a regular expression as an argument that modify the current buffer.

Place the cursor before the lines you want to filter and run M-x keep-lines with argument ^[A-Z ]+$ to get the result you want.

  • This answer is better. Any idea why does M-x flush-lines with argument [^A-Z ] and [^A-Z \n] also flush the lines of uppercase letters and spaces? Am I missing something? My Emacs version is 26.1. – whatacold Dec 31 '18 at 15:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.