Extract string between two special chracters

Hi Folks -

I'm trying to extract the string between two special characters, the "-" and "." symbols.

The string format is as such:

_PBCS_URL_PRD=https://plan-a503777.pbcs.us6.ocloud.com
_PBCS_URL_TST=https://pln-test-a503777.pbcs.us6.ocloud.com

In the above case, I need to extract "a503777".

Notice _PBCS_URL_TST has two "-" symbols.

I've tried the following with no success:

echo "${_PBCS_URL_PRD}" | sed -e 's/-\(.*\)./\1/g'

As always:
What operating system are you using?

What shell are you using?

Does this code produce the result you want?

echo "${_PBCS_URL_PRD}" |  perl -nle '/-(\w+)\./ and print $1'

Hi Don -

BASH.

Thanks!

Try

echo "${_PBCS_URL_TST}" | sed -e 's/^.*-\|[.].*$//g'
a503777

With bash or ksh or any other POSIX-conforming shell, you can use:

_PBCS_URL_PRD=https://plan-a503777.pbcs.us6.ocloud.com
_PBCS_URL_TST=https://pln-test-a503777.pbcs.us6.ocloud.com

STRING=${_PBCS_URL_PRD##*-}
echo ${STRING%%.*}

STRING=${_PBCS_URL_TST##*-}
echo ${STRING%%.*}

producing the output:

a503777
a503777

without needing to invoke any utilities that are not built into the shell itself. (Although there is no requirement that echo be built into the shell, almost all recent shells do provide it as a built-in.)

Hi Don -

Thank you so much!

That worked like a charm and it's great to know its adaptable across a few different OS's.

_PBCS_URL=${_PBCS_URL_TST}; STRING=${_PBCS_URL##*-}; _DOMAIN=${STRING%%.*}

RudiC and Aia, both of those options worked great too!

use SED, stream editor with regular expression:

$ echo _PBCS_URL_TST=https://pln-test-a503777.pbcs.us6.ocloud.com |sed -E 's/.*?-([^.]+)\..*/\1/g'