malmo
malmo

Reputation: 524

extract a pattern if a number is higher than a specific integer using sed or awk

I want to extract the lines that contain numbers which exceed a specific Integer for example if I have the following code

INTEGER ( 16 )
INTEGER ( 16 )
INTEGER ( 6 )
INTEGER ( 18 )

I want to keep only the lines that contain INTEGER (n <= 16), so I want to have as an output

INTEGER ( 16 )
INTEGER ( 16 )
INTEGER ( 6 )

Upvotes: 0

Views: 62

Answers (1)

Tom Fenech
Tom Fenech

Reputation: 74595

If you can be sure that there are always spaces before and after the digits, then you could use this awk:

awk '$3 <= 16' file

This simply checks whether the third field is less than or equal to 16.

However, it might be safer to use something like this:

awk -F'[^0-9]+' '/INTEGER *\( *[0-9]+ *\)/ && $2 <= 16' file

This sets the field separator to any number of non-digit characters, so the first field is empty and the second field contains the digits you're interested in. If the line matches the pattern (which is flexible with respect to spacing) and the digits are less than or equal to 16, the line is printed.

Upvotes: 2

Related Questions