user3698088
user3698088

Reputation: 1

Using grep to find lines between a set of numbers

I'm doing a Unix assignment for my class and I've run into a bit of a problem. We are trying to output lines that have a number between 20-30.

Is there a way to use grep or egrep so that you can output a line that has a number that has limits, such as a number between 20 and 30 or a number less than 25?

Upvotes: 0

Views: 816

Answers (4)

cucurbit
cucurbit

Reputation: 1442

You can try this command:

 grep -wE '\b[2]{1}[0-9]{1}\b|30' file

Explanation:

[2]{1} --> find a number starting with one 2
[0-9]{1} --> The "2" is follored by any number but only repeated once
|30 --> And also finds "30" number.

Upvotes: 0

Kaze..Sujan
Kaze..Sujan

Reputation: 116

to grep for a value between 20-30 you can use this

egrep -e "[^0-9]2[0-9][^0-9]" -e "[^0-9]30[^0-9]" <file_name>

Upvotes: 0

Kalanidhi
Kalanidhi

Reputation: 5092

You can use this simple grep with regular expression

$-grep  -wE '(2[0-9]|30)' file_name

Upvotes: 1

perreal
perreal

Reputation: 97938

This should do:

grep '\(^\|\D\)\(2[0-9]\|30\)\(\D\|$\)' input

or, similarly:

grep '\(^\|[^0-9]\)\(2[0-9]\|30\)\([^0-9]\|$\)' input

or with the -P flag:

grep -P '(^|[^0-9])(2[0-9]|30)([^0-9]|$)' input

so basically, think of the possible strings you want to match and express them as patterns.

Upvotes: 1

Related Questions