I need to check the condition of a variable before the script continues and it needs to match a specific pattern such as EPS-03-0 or PDF-02-1.
The first part is a 3 or 4 letter string followed by a hyphen, then a 01,02 or 03 followed by a hyphen then a 0 or a 1.
I know I could check for every possible combination, but I was hoping there was an easier (and more scalable) way to proceed.
Thanks in advance for any help rendered.
first I just tried to check for the pattern and failed i.e.
if ( "$4" = "???(?)-[01-03]-[0-1]" ) ; then
echo "Matched" > testing
else
echo "Mismatch" > testting
fi
I obviously don't know what I 'm doing.
Following should be the right regex: ^[A-Z]{3,4}-0[123]-[01]$
I guess you get the idea and can adapt it for your needs.
$ cat vars
EPS-03-0
ABAC-01-3
PDF-02-1
ZY-021-0
PERL-03-1
BASHH-01-0
XYZ-02-02
PNG-01-2
$
$ while read line
> do
> if [[ "$line" =~ ^[A-Z]{3,4}-0[123]-[01]$ ]]; then
> echo "match"
> else
> echo "mismatch"
> fi
> done < vars
match
mismatch
match
mismatch
match
mismatch
mismatch
mismatch
$
thank you so much for your quick reply, and it seems to be working perfectly!!!
but I'm curious and if you wouldn't mind answering a few questions I'm having trouble finding the answers on my own.
I understand that =~ allows for the regexp you use, but what are the purposes of the carat (^) and dollar ($) ? I thought the ^ meant 'not'.
thanks again
In an RE, ^
matches the start of a line/string and $
matches the end of a line/string.
thanks!!
you guys are the best!!!
saved me SO MUCH TIME!!!