Match the lines with the pattern n times in one line

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

foo foo  
foo  
foo foo foo   

I could do this with a regex in vim or sed, awk or any other tool. I searched googled and did not find anyone to do this. It will probably do a script or something similar to parse every line. Has anyone done this?

thank

+3
source share
6 answers

Single Line Perl:

perl -ne 'print if(tr/o/o/ == 4)' foo_file
+3
source

, :

(?=(.*o){4})(?!(.*o){5,}).*

Regexr - http://regexr.com?2toro

, . , foos , :

(?=(.*foo){4})(?!(.*foo){5,}).*

Regexr - http://regexr.com?2tosa

+4
perl -lnwe '@c=$_=~/o/g;if(scalar(@c)==4){print $_}' file_to_parse
+3

awk...

awk '{ if (gsub(/o/, "o") == 4) print }' # lines that matched
awk '{ if (gsub(/o/, "o") != 4) print }' # lines that didn't

/, , - ...

awk -v pattern=o -v matches=4 '{ if (gsub(pattern, pattern) == matches) print }'
+2

, .

^[^o]*o[^o]*o[^o]*o[^o]*o[^o]*$. " " ( ), "" , " " .

- . , "foo" , "f" "fo", "foo" . , "foo" , "ffofofoofoffoffoofoffofofo", .

", " foo ", ([^f]|f[^o]|fo[^o])*, " f "" fo " , " foo ". , , , .

-1

All Articles