Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emacs: search a buffer with regex and show results in a new buffer?

Tags:

regex

emacs

Supposed I had a buffer in emacs with 1000 lines, and dotted around in that buffer were unicode references (eg. \u8226). I want to collect all those references into another buffer, which I can then sort and uniquify.

On the (*nix) command line, I can do something like:

grep -o "\\\\u[0-9]*" tmpfile | sort | uniq

Is this achievable directly within emacs, without saving any buffers to disk?

like image 467
Phillip B Oldham Avatar asked Dec 16 '22 14:12

Phillip B Oldham


2 Answers

Go to your buffer, select it all with C-x h, then do M-| grep -o "\\\\u[0-9]*" | sort | uniq (this runs shell-command-on-region). The output will go to *Shell Command Output*, which will not be shown unless the output is long enough, but it's always created and you can switch to it regardless.

There is also occur (M-s o).

like image 119
jpkotta Avatar answered Jan 11 '23 23:01

jpkotta


M-x occur RET [[:nonascii:]]+ RET
  M-x other-window
  M-x toggle-read-only
  M-x sort-lines
like image 23
aartist Avatar answered Jan 11 '23 23:01

aartist