Skip to content
Advertisement

how to use IF to test if a value meets a certain mask

I have a variable and I need to test if it contain a formated value like this ……..-….-….-….-…………

the variable can contem any value in any format, and I need to test if the value is like 1572C097-4452-4495-8369-C7606F2C867E

I tried using sed to retrieve the value from another variable

tguid=$(echo $i |sed 's/.*(........-....-....-....-............).*/1/')

But now, I dont have a clue how can I use if [] statement to guarantee if the variable tguid have the correct value?

Advertisement

Answer

You don’t need to use sed. A conditional expression can test whether a string matches a regexp directly.

if [[ $i =~ .{8}-.{4}-.{4}-.{4}-.{12} ]]
then
    echo It matches
else
    echo It does not match
fi
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement