Get substring after a special character

I have many strings that look like the following:

word1.word2.word3.xyz
word1.word2.word3.word4.abc
word1.word2.mno
word1.word2.word3.pqr

Using bash, I would like to just get the string after the last ‘.'(dot) character.
So the output I want:

xyz
abc
mno
pqr

Is there any way to do this?

>Solution :

One simple solution would be to split the string on . and then get the last item from the splitted array

LINES=(word1.word2.word3.xyz word1.word2.word3.xyz word1.word2.word3.word4.abc word1.word2.mno word1.word2.word3.pqr)

# for loop on str
for LINE in ${LINES[@]}
do
    LINE_SPLIT=(${LINE//./ })
    echo ${LINE_SPLIT[-1]}
done

Leave a Reply