I would go with something a little more powerful than grep
. This can do it in perl:
perl -ne 'print if s/\d+/$&/g == 5' your_file
The regex substitution replaces all groups of one or more digits (\d+
) with themselves ($&
): it does nothing. It is used merely for side effect since the s///
operator returns the number of times it managed to substitute for its regex. Thus, the line is printed only if s///
found 5 groups of digits.