简体   繁体   中英

emacs: inverse search

Is there a way to do a inverse search? I have very big log file where a particular pattern fills up for few dozen pages

20100414 alpha beta
20100414 alpha beta
<few dozen pages>
20100414 alpha beta
20100414 gamma delta
20100414 gamma delta
<few dozen pages>
20100414 gamma delta

Problem is, I don't know what text would be after "alpha beta". It could be "gamma delta" or something else. So I would like to skip all the lines that contain "alpha beta".

Two ideas:

  1. Mx keep-lines <RET> REGEXP <RET>

    will remove all lines not matching a regexp

  2. Mx grep <RET> grep -nH -e "<REGEXP>" -v <FILE>

    will find all lines in NOT containing your regexp.

You could use hide-lines: http://www.emacswiki.org/emacs/hide-lines.el

Then Mx hide-lines RET alpha beta RET will hide all lines containing "alpha beta".

Now you can search using eg Cs ...

In general you can't do an inverse search, but for your particular case you could use a simple function:

(defun my-skip-lines-matching-regexp (regexp)
  "Skip lines matching a regexp."
  (interactive "sSkip lines matching regexp: ")
  (beginning-of-line)
  (while (and (not (eobp)) (looking-at regexp))
    (forward-line 1)))

then put in ".+alpha beta" for the regexp.

I usually solve this by using a regexp search

C-u C-r ^20100414 [^a]

which searches for the next line that is "20100414 ", and that does the trick most of the time. It'd find the "gamma delta" line, but would obviously miss a line that looks like "20100414 allegro".

There is also the command Mx flush-lines RE , which gets rid of all lines that match the regular expression RE . This does modify the buffer.

A heuristic method that often proves to be useful is to jump to the end of the file and then search backward for the text you want to skip. The success of this method obviously depends on the content of the file and works best when the repeating text in question occurs in singular chunks.

You can also search using grep , specifying that you want the lines that do not match.

You can also search using icicle-occur , using C-~ to remove lines that match whatever you type.

http://www.emacswiki.org/emacs/Icicles_-_Search_Commands%2c_Overview

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM