0

Can some body pl tell me why this if condition doesn't output Matched and how to change the reg exp pattern with expr to display the output as Matched. The thing is that instead of BH in the var variable there can be any country code like US or CA. All the other characters in the variable remains the same.

var1="BH.EBS.EBS.BH.RCMS.RCMS.FBACCR"
if [ `expr $var1 : "*.EBS.EBS.*.RCMS.RCMS.FBACCR"` -gt 0 ]; then
echo "Matched"
else
echo "Not matched"
fi

Thanks Gautam

Gautam S
  • 41
  • 1
  • 1
  • 7

1 Answers1

1

Something like this would do it:

.\.EBS\.EBS\...\.RCMS\.RCMS\.FBACCR

Just keep in mind that the dots in your original string are a special type of character in a regexp, this means they won't be interpreted as a dot. It says instead that is could be any char. Then you have to scape them, hence the backslash.

Finally where you need to match BH, you can use dots .. if the country codes are guaranteed to be 2 chars long. If not, you can use instead:

.*\.EBS\.EBS\..*\.RCMS\.RCMS\.FBACCR
xiumeteo
  • 941
  • 7
  • 16
  • So in other words, the command accepts a POSIX Basic Regular Expression, but OP is trying to use it with a shell glob. The solution is to rewrite the glob into a compatible regex. – that other guy Jul 18 '20 at 00:44
  • 1
    Hi @xiumeteo and @the other guy Yes the country codes will always be 2 characters. i included a dot before the * at the start of the comparison and it seems to work. Is this proper way to doing this ? var1="BH.EBS.EBS.BH.RCMS.RCMS.FBACCR" if [ `expr $var1 : ".*.EBS.*.RCMS.FBACCR"` -gt 0 ]; then echo "Matched" else echo "Not matched" fi – Gautam S Jul 18 '20 at 01:25