user1551817
user1551817

Reputation: 7471

Only output values within a certain range

I run a command that produce lots of lines in my terminal - the lines are floats.

I only want certain numbers to be output as a line in my terminal.

I know that I can pipe the results to egrep:

| egrep "(369|433|375|368)"

if I want only certain values to appear. But is it possible to only have lines that have a value within ± 50 of 350 (for example) to appear?

Upvotes: 0

Views: 88

Answers (2)

Mark Setchell
Mark Setchell

Reputation: 207668

I would go with awk here:

./yourProgram | awk '$1>250 && $1<350'

e.g.

echo -e "12.3\n342.678\n287.99999" | awk '$1>250 && $1<350'

342.678
287.99999

Upvotes: 0

G Fetterman
G Fetterman

Reputation: 456

grep matches against string tokens, so you have to either:

  1. figure out the right string match for the number range you want (e.g., for 300-400, you might do something like grep -E [34].., with appropriate additional context added to the expression and a number of additional .s equal to your floating-point precision)
  2. convert the number strings to actual numbers in whatever programming language you prefer to use and filter them that way

I'd strongly encourage you to take the second option.

Upvotes: 1

Related Questions