I have a file and I need to filter lines that have (or don't have) N occurrences of a pattern.
I.e., if my pattern is the letter o and I what to match lines where the letter o occurs exactly 4 times, the expression should match the first of the following example lines but not the others:

foo foo
foo
foo foo foo

I thouth I could do it with a regex in vim, or sed, awk, or any other tool.
I've googled and haven't found anyone that has done a similar thing.
Probably will have do a script or something similar to parse each line.
Does anyone have done a similar thing?

If you want to write code, then you can construct a DFA based string matching or i would tell you to have a look at the shift or string matching algorithm, which you can easily write. Then you can input the string to the proper datastructure as per the algorithm needs. Read http://en.wikipedia.org/wiki/Shift_Or_Algorithm for the shift-or string matching algorithm.

For the single letter case, an expression such as ^[^o]*o[^o]*o[^o]*o[^o]*o[^o]*$ would work. It basically looks for "not o" (zero or more) followed by "o" four times, and allows extra "not o" characters at the end.

But longer expressions are bit of a problem. For example, in order not to find the word "foo", you have to allow "f" and "fo" but not "foo". So to find a line with exactly twice "foo", you have to allow the line "ffofofoofoffoffoofoffofofo" which is not so easy to define.

To match "anything but 'foo'" you could use the expression ([^f]|f[^o]|fo[^o])* which allows "f" and "fo" and other things, but not "foo". But you can see how this can become annoying if the word is longer and you have to match it four times.