Home > OS >  how to extract part of URL in bash using regx?
how to extract part of URL in bash using regx?

Time:10-20

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??

CodePudding user response:

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.

CodePudding user response:

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/
#          ~

CodePudding user response:

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

CodePudding user response:

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/")

CodePudding user response:

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.

CodePudding user response:

Try awk:

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