Extract lines that have two or more dots
\.
and[.]
are equivalent — they both match a literal dot, and not any other character. As a matter of style, pick one and use it consistently.- Your problem is that your regular expression (i.e., pattern)
has
([^.]+\.)+
followed by[.]+
. That’s really (sort of) equivalent to[^.]+\.
followed by[.]
, with the result that your grep is looking for lines that containtext.text..
, i.e., two dots in a row. If you check, you’ll see that your command matchesa.b..
. - OK, I believe that the fix is fairly simple:
grep -P '^[^.]+\.([^.]+\.)+[^.]*$'
I.e., change the[.]
to[^.]
(perhaps that’s what you meant originally?), change the following+
to an*
, and add a$
. After some number oftext.
groups, require/allow any number (zero or more) characters other than dot, up to the end of the line. - An even simpler approach (easier to understand) would be
grep -P '^[^.]+\..*\.' file.txt | grep -v '\.\.'
The firstgrep
finds lines that begin with a non-dot character and include at least two dots. The secondgrep
removes lines that have two consecutive dots. - Rather than do
grep … | wc -l
, just dogrep -c …
.