1833062 Members
3093 Online
110049 Solutions
New Discussion

Regexp range

 
SOLVED
Go to solution
Petra Sandberg
Advisor

Regexp range

Hi, does anyone have a suggestion on how to match a number serie ranging from for exampel 17585 to 17643 in a regexp.

Regards Petra
8 REPLIES 8
Robert-Jan Goossens
Honored Contributor

Re: Regexp range

Hi Petra,

try something like this.

# grep "17[5-6][0-9][0-9]" file

Regards,
Robert-Jan
harry d brown jr
Honored Contributor
Solution

Re: Regexp range


Robert-Jan,

The statement

grep "17[5-6][0-9][0-9]" file

will also include the numbers 17500-17584 and 17644-17699

thus they need to include grep -v 's

# grep "17[5-6][0-9][0-9]" file | grep -v -e "175[0-7][0-9]" -e "1758[0-4]" -e "1764[4-9]" -e "176[5-9][0-9]"


live free or die
harry d brown jr

Regards,
Robert-Jan
Live Free or Die
G. Vrijhoeven
Honored Contributor

Re: Regexp range

No gerexpr, but:

for i in `cat file`
do
if [ "$i" -gt 17584 ]
then
if [ "$i" -lt 17644 ]
then
echo $i
fi
fi
done

HTH,

Gideon

Petra Sandberg
Advisor

Re: Regexp range

Thanks for the reply, but it's not exactly what I want. The problem is this I want to match just between 17585 to 17643 this gives a match on 17644 to 17699 as well.

Petra
harry d brown jr
Honored Contributor

Re: Regexp range

Petra:

[root@vpart1 /tmp]# cat xyz
17499
17500
17540
17580
17584
17585
17586
17600
17640
17642
17643
17644
17650
17800
[root@vpart1 /tmp]# grep "17[5-6][0-9][0-9]" xyz | grep -v -e "175[0-7][0-9]" -e "1758[0-4]" -e "1764[4-9]" -e "176[5-9][0-9]"
17585
17586
17600
17640
17642
17643
[root@vpart1 /tmp]#


live free or die
harry d brown jr
Live Free or Die
Robert-Jan Goossens
Honored Contributor

Re: Regexp range

Hi Petra,

Check Harry's second option, he allready added the grep -v options to remove the ranges.

Robert-Jan

Ps
Thanks Harry for the correction.
Petra Sandberg
Advisor

Re: Regexp range

Thank you all for your replies. The case is solved.

Regards Petra
H.Merijn Brand (procura
Honored Contributor

Re: Regexp range

She asked "IN" a regex :)

# echo "17485" | perl -nle'/\b(17\d\d\d)\b(??{$x=$^N>=17585&&$^N<=17643?"":"(?<=X)"})\b/ and print "IN"'

# echo "17585" | perl -nle'/\b(17\d\d\d)\b(??{$x=$^N>=17585&&$^N<=17643?"":"(?<=X)"})\b/ and print "IN"'
IN

OK, that's dirty!

# echo "17585" | perl -nle'/\b17(5(8[5-9]|[6-9]\d)|6([0-3]\d|4[0-3]))\b/ and print "Match!"

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn