dain
dain

Reputation: 51

Linux grep - print numbers from file in x to y

I just asked how to print from -10 and 10, although I understand it now, I have no understanding how I could print from a different range, eg. from -8 to 23.

What I first did

egrep '^-?[0-8]?[0]?[1-9]$' numbers.txt

Prints from -24 to 24

egrep '^[-]?[0-8]$+\.?' numbers.txt

Prints from -8 to 8.

How could I combine each other so the result would be -8 .. 23?

Upvotes: 0

Views: 270

Answers (2)

repzero
repzero

Reputation: 8412

My version

egrep --color '[-][1-8]|([0]|[1])[0-9]|[2][0-3]'

[-][1-8] # -1 to -8

([0]|[1])[0-9] # 0-19

[2][0-3] #20-23

Upvotes: 0

fedorqui
fedorqui

Reputation: 289725

You can for example say:

egrep '^(-?0?[0-8]|9|1[0-9]|2[0-3])$'

This uses a ^(option1|option2|...|option_n)$ to match the following cases:

  • -?0?[0-8] -8 to 8
  • 9 9
  • 1[0-9] 10 to 19
  • 2[0-3] 20 to 23

Upvotes: 2

Related Questions