Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading