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 pa开发者_如何学Pythonges>
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:
M-x keep-lines <RET> REGEXP <RET>
will remove all lines not matching a regexp
M-x 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 M-x hide-lines RET alpha beta RET
will hide all lines containing "alpha beta".
Now you can search using e.g. C-s
...
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 M-x flush-lines RE, which gets rid of all lines that match the regular expression RE
. This does modify the buffer.
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
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.
精彩评论