JDS
JDS

Reputation: 16998

Get substring after a dot character, compare

I have:

string1="start1.MATCH.ext"
string2="start2.qwer.ext"
string3="start3.MATCH.ext"

...

I want to take every string and extract the portion after the first period, so

MATCH.ext
qwer.ext
MATCH.ext

And then compare these substrings such that I can do something in an if-statement if they match:

if (substr[i] == substr[j]) //now I can do something with string1 and string3

How can I do this in Bash?

Upvotes: 2

Views: 1377

Answers (1)

Kevin
Kevin

Reputation: 56129

You can chop the front of a string using ${var#*.}, and compare with if [[ ... ]]:

string1="start1.MATCH.ext"
string2="start2.qwer.ext"
string3="start3.MATCH.ext"

if [[ ${string1#*.} = ${string3#*.} ]]; then
        echo They match
else
        echo No match
fi

Upvotes: 5

Related Questions