1

I am trying to extract part of url using bash regex . from below mentioned URL i just want to extract 124, which will always be between two hyphens .

http://abc-124-001.portal-ex.xyz-xyz.com:8091/

i tried doing following but i am looking for any better options to do this in one line

f=http://abc-124-001.portal-ex.xyz-xyz.com:8091/
f=${f#*-}
echo ${f%%-*}

op: 124

can this be done in one line or in any better way??

2
  • if all you want is a one-liner then consider: f=${f#*-}; echo ${f%%-*} Commented Oct 19, 2022 at 16:20
  • Do you need more help or is there a solution that you liked best? Commented Oct 20, 2022 at 10:51

6 Answers 6

1

You can use cut:

#!/bin/bash
s='htt''p://abc-124-001.portal-ex.xyz-xyz.com:8091/'
id=$(cut -d'-' -f2 <<< "$s")
echo "$id"

See the online demo.

Sign up to request clarification or add additional context in comments.

4 Comments

Hi @Wiktor , How can i extract "abc-124" from host rather then just "124" ?
echo 'abc-124-001.portal-ex.xyz-xyz.com:8091'| cut -d"/" -f2 | cut -d"-" -f1 this doesn't work .. what am i doing wrong here?
envName=abc-124-001.portal-ex.xyz-xyz.com:8091 ... envName=${envName%%.*} ... envName=${envName%-*} .... echo $envName | cut -d"/" -f3 ........ abc-124 .... can this be done in better way?
@deewreck There are many ways. Here is one: id=$(cut -d'-' -f1,2 <<< 'htt''p://abc-124-001.portal-ex.xyz-xyz.com:8091/') and then echo "${id/http:\/\//}", or use id=$(sed 's,.*://\([^-]*-[^-]*\).*,\1,' <<< 'htt''p://abc-124-001.portal-ex.xyz-xyz.com:8091/') (demo).
1

I think your approach is not bad. Alternatively this can also be done using a regexp:

[[ $f =~ -([^-]+)- ]]
echo ${BASH_REMATCH[1]}

It needs only "one step" for the extraction, while your approach needs two. However your glob patterns are simpler than my regex, so it is a matter of taste what you consider simpler.

Note that I didn't treat the case when the pattern does not match at all, simply since you also did not care about this case. It is trivial to add some error handling if you want to.

Comments

1

You can use regex matching:

if [[ $f =~ -([0-9]+) ]] ; then
    n=${BASH_REMATCH[1]}
    echo $n
else
    echo Cannot match in $f >&2
    exit 1
fi

Or maybe ^[^-]+-([0-9]+) to not match 124 in

http://abc-a-124-001.portal-ex.xyz-xyz.com:8091/
#          ~

Comments

1

Not really a one-liner but almost:

$ f=http://abc-124-001.portal-ex.xyz-xyz.com:8091/
$ arr=(${f//-/ })

What you look for is arr[1]:

$ echo ${arr[1]}
124

Comments

1

Using read with IFS

$ f=http://abc-124-001.portal-ex.xyz-xyz.com:8091/
$ IFS='-' read -ra parts <<< $f
$ echo "${parts[1]}"
124
$ declare -p parts
declare -a parts=([0]="http://abc" [1]="124" [2]="001.portal" [3]="ex.xyz" [4]="xyz.com:8091/")

Comments

1

Try awk:

$ echo "http://abc-124-001.portal-ex.xyz-xyz.com:8091/" | awk -F"-" ' { print $2 } '
124

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.