Regex for for integer from 0-7 except 2

Hi,

I am trying to use a regular exp to match any number between 0 and 7 except 2......
I am using:

echo $myvar | egrep "{1}[0-1]{1}[3-7]"

Which isnt working....

Please can someone help?
Thanks

yonderboy

This is the answer you are seeking

egrep -v "[289]"

You can try in Sed

$cat file_name
0
1
2
3
4
5
6
7
sed -n '/2/n; /[0-7]/p' file_name
0
1
3
4
5
6
7

Fantastic, thank you. My code is working.

---------- Post updated at 03:20 PM ---------- Previous update was at 03:08 PM ----------

Just out of interest, can you explain how the first solution works?

The option -v make grep to search for everything that don't match the regexp, in this case, it prints every line that not contains chars 2, 8 or 9

grep '[013-7]'

js.

Hahaha. this also matches everything else under the sun, including:

jdlkflkflkdl320kkjkjlk&%#jkvjkjfdfdfd

:smiley:

.... which is not what the original poster asked for :wink:

---------- Post updated at 15:59 ---------- Previous update was at 15:53 ----------

The regex to match 0-7 but not any 2 is:

[013-7]

to match one or more of the above, use:

[013-7]+

haha, ok, thanks Neo. I didn't test my code with anything other than numbers from 0-10 so I didn't notice this. I've amended it now. Thanks.

People creating regex should keep in mind that NOT DOG does not necessarily mean CAT. NOT DOG also matches ROCK, CARROT, GEM STONE, BASEBALL, 1458, $12.45, etc.

Neo,

Your code is fine for single digit numbers but I've just discovered that your code will match any number with more than 1 digit, for example 10, 100, 1000 ...can you help?

Thanks

$ 
$ seq -5 15 >f5
$ seq 1 10 | perl -lne 'printf("%c\n",$_+96)' >>f5
$ 
$ cat f5
-5
-4
-3
-2
-1
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
a
b
c
d
e
f
g
h
i
j
$ 
$ # fetch only those lines from file "f5" that have a *single* digit from 0 to 7 (both inclusive) except 2
$ grep "^[013-7]$" f5
0
1
3
4
5
6
7
$ 

tyler_durden