With `GNU grep` or [ripgrep](https://github.com/BurntSushi/ripgrep) $ LC_ALL=C grep -c $'\t''0\b.*\b0\b.*\b0\b' ip.txt 7 $ rg -c '\t0\b.*\b0\b.*\b0\b' ip.txt 7 where `$'\t'` will match tab character, thus working even if first column is `0`. Remove `LC_ALL=C` if file can contain non-ASCII characters. `ripgrep` is usually faster than `GNU grep` but in test run `GNU grep` was faster, likely because `\b` in `ripgrep` is always unicode based.