Hi,
I'm trying to match a filename that could be called anything from vout001 to vout252 and was trying to do a small test but I'm not getting the result I thought I would..
Can some one tell me what I'm doing wrong?
*****@********[/home/aiw1]>echo $mynumber
vout123
*****@********[/home/aiw1]>if [ $mynumber == 'vout[0...252]' ]
> then
> echo 't'
> else
> echo 'f'
> fi
f
Ranges are charracter classes in regular expressions, not integer values, the range you created is 0..252 ie. the set (0,1,2,5,2) however ranges need to be used in a suitable tool rather than directly in the shell
One, horrendously complex, way of doing it would be to capture the digits and assess them separately like below, there is probably a much neater solution though...
$ myfile=vout123
$ export myfile
$ perl -e 'if ($ENV{myfile}=~/vout(\d+)/ && $1 >0 && $1 <253){print "t\n";}'
t
$
There are several different kinds of confusion in there.
You cannot use == inside [ ] brackets, it needs [[ ]] brackets.
And == does not understand number ranges anyway. It understands characters.
Try a case statement:
case "$VAR" in
abcd[0-9]) echo stuff ;;
abcd[1-9][0-9]) echo stuff ;;
abcd1[0-9][0-9]) echo stuff ;;
abcd2[1-4][0-9]) echo stuff ;;
abcd25[0-2]) echo stuff ;;
*) echo "not matched" ;;
esac
Or you could try stripping out the number first and just comparing that.
1 Like
Hello,
One more approach with awk
.
echo $VAR | awk '/[a-z]{3}[0-9]{3}/ {print "Matched"}; /![a-z]{3}[0-9]{3}/ {print "NOT matched"}'
where var
variable have the values.
Thanks,
R. Singh
Perhaps like this:
$ echo $mynumber
vout123
$ if [ ${mynumber#????} -ge 0 ] && [ ${mynumber#????} -le 252 ]
> then
> echo 't'
> else
> echo 'f'
> fi
t