Shred
Shred

Reputation: 358

Get only lines whose value respects range in bash

Guys I've got a text file with this scheme (just few lines of the original 20k lines):

Skeet: 120  Mark: 3636  Tatos: 502
Skeet: 190  Mark: 764  Tatos: 468  
Skeet: 210  Mark: 2106  Tatos: 302
Skeet: 305  Mark: 510  Tatos: 1900     

What i want to do is to print only lines where value of "Skeet" is from 150 to 240. Remember that lines are more than 10k, so it needs to be inserted into a loop to make sure it reads every line. Desidered output:

Skeet: 190  Mark: 764  Tatos: 468  
Skeet: 210  Mark: 2106  Tatos: 302

Upvotes: 3

Views: 64

Answers (2)

HardcoreHenry
HardcoreHenry

Reputation: 6377

awk '{if ($2 >= 150 && $2 <= 240) print}' file.txt

For setting the limits through bash variables you could do something similar to what @RavinderSingh13 pointed out:

awk -v start=$foo -v end=$oof '{if ($2 >= start && $2 <= end) print}' file.txt

Upvotes: 1

RavinderSingh13
RavinderSingh13

Reputation: 133438

As per OP if you want to have shell variables and values of comparisons in variables then following could help you:

var="Skeet:" ##Shell variable
awk -v val="$var" -v start=150 -v end=240 '$1==val && ($2>=start && $2<=end)'  Input_file

Upvotes: 4

Related Questions