'Unix Regex (ERE) for finding a lines with 4 or more numbers

I've tried so many variations but none seem to work, I have no idea what I don't understand.

The last one I've tried is (\<[[:digit:]]+\>.*){4,}. But it doesn't even find lines like 123 123 123 123. The input lines can be anything (even like hello 123 my 1 name 2 is 3).

I didn't specify it, sorry, but what I meant is: line "123 a2" has 1 number, line "1 2 3 45" has 4 numbers.



Solution 1:[1]

With your shown samples, please try following awk code. We need NOT to use loop here. This awk code is written and tested in GNU awk.

awk -v FPAT='(^|[[:space:]]+)[0-9]+([[:space:]]+|$)' 'NF>3' Input_file

Explanation: Adding detailed explanation for above.

  • Using GNU awk's option named FPAT to allow regex to make field separators.
  • Using regex (^|[[:space:]]+)[0-9]+([[:space:]]+|$) to match either starting spaces followed by digits OR digits followed by spaces or ending of line.
  • In main awk program checking condition NF>3 which means if number of fields are greater than 3 then print that line.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1