Regex with negative matching (ie, find string that _doesn't_ match regex)?

Use the :g! Command to delete every line that doesn't match.

Up vote 10 down vote favorite 3 share g+ share fb share tw.

I have a log file with the string "ERROR" on some lines. I want to delete every line that doesn't have ERROR so that I can see just what needs fixing. I was going to do something like the following in vim: %s/!(ERROR)// to replace non-error lines with an empty string.

I do not believe that standard regexes can do this, but maybe I'm wrong... regex vim vi link|improve this question edited Jul 22 '09 at 0:41too much php15k74568 asked Jul 22 '09 at 0:10Thr4wn1,9801734 91% accept rate.

Use the :g! Command to delete every line that doesn't match. :g!

/ERROR/d.

8 :v/ERROR/d is equivalent – rampion Jul 22 '09 at 2:05.

In vim, you can run any filter command on the text in the buffer. For example, :%! Grep ERROR will replace the entire buffer with only the lines that match the given regular expression.

This is useful for more than just grep, for example you can sort the lines in the buffer with :%!sort. Or, you can do the same for any range of text using the V command to mark the block and then :! Filter-command (vim will automatically fill in ' for you to indicate the currently marked block).

1 for a command that doesn't require leaving vim / redirecting into a different file – too much php Jul 22 '09 at 1:01 Maybe not the best of examples, as :grep and :sort do have built-in meanings in Vim (which are different than :! Grep and :! Sort, just to be confusing).

– ephemient Apr 5 '10 at 19:04.

If you're on a Unix machine (sounds like you are) you could just use grep -v ERROR yourfilename > newfile.

A negative matching regex will use ^ For eg. ^E will match everything except E.

1 That's true, but such a regex will match every character that's not E, which is different from matching all lines that do not contain E. Also, such a negative match is awkward to extend beyond one character. – Greg Hewgill Jul 22 '09 at 0:41.

If on *nix, you can use grep -v or awk awk '! /ERROR/' file | more on a windows machine, you can use findstr findstr /v /c:ERROR file | more.

1 Not a very useful answer without an example: :%s/^\(\ERROR\)\@! $\n//g. In Vim the \@!

Is the negative lookahead indicator, not the (?! ) format from that link. The :g!

Answer is easier though. – atomicules Feb 9 '11 at 14:36.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions