tags:

views:

95

answers:

4

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".

+1  A: 

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.

Trey Jackson
Hey, how did you format that keyboard-shortcut box thingy?
Ryan Thompson
@RyanThompson You surround the text with a 'kbd' enclosure like so: <kbd>something</kbd>.
Trey Jackson
+2  A: 

Two ideas:

1) M-x keep-lines <RET> REGEXP <RET>

will remove all lines not matching a regexp

2) M-x grep <RET> grep -nH -e "<REGEXP>" -v <FILE>

will find all lines in NOT containing your regexp.

ostolop
FWIW `flush-lines` does the opposite of `keep-lines` i.e. it deletes matching lines.
Ivan Andrus
A: 

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.

scottfrazer
A: 

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...

slu
It's similar to keep-lines and flush-lines, but it does not modify the buffer.
slu