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:
o
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
Single Line Perl:
perl -ne 'print if(tr/o/o/ == 4)' foo_file
, :
(?=(.*o){4})(?!(.*o){5,}).*
Regexr - http://regexr.com?2toro
, . , foos , :
(?=(.*foo){4})(?!(.*foo){5,}).*
Regexr - http://regexr.com?2tosa
perl -lnwe '@c=$_=~/o/g;if(scalar(@c)==4){print $_}' file_to_parse
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 }'
, DFA, , . . http://en.wikipedia.org/wiki/Shift_Or_Algorithm .
, .
^[^o]*o[^o]*o[^o]*o[^o]*o[^o]*$. " " ( ), "" , " " .
^[^o]*o[^o]*o[^o]*o[^o]*o[^o]*$
- . , "foo" , "f" "fo", "foo" . , "foo" , "ffofofoofoffoffoofoffofofo", .
", " foo ", ([^f]|f[^o]|fo[^o])*, " f "" fo " , " foo ". , , , .
([^f]|f[^o]|fo[^o])*