2

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
Heikki
  • 2,961
  • 11
  • 18
a_subscriber
  • 3,854
  • 1
  • 17
  • 47

2 Answers2

2

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.

Heikki
  • 2,961
  • 11
  • 18
  • 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
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))))
Navy Cheng
  • 126
  • 2