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

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
deewreck
  • 113
  • 7

6 Answers6

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.

user1934428
  • 19,864
  • 7
  • 42
  • 87
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/
#          ~
choroba
  • 231,213
  • 25
  • 204
  • 289
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.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • Hi @Wiktor , How can i extract "abc-124" from host rather then just "124" ? – deewreck Nov 09 '22 at 07:03
  • echo 'http://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? – deewreck Nov 09 '22 at 07:13
  • envName=http://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 Nov 09 '22 at 07:37
  • @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](https://ideone.com/p6Sjbv)). – Wiktor Stribiżew Nov 09 '22 at 08:59
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
Renaud Pacalet
  • 25,260
  • 3
  • 34
  • 51
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/")
glenn jackman
  • 238,783
  • 38
  • 220
  • 352
1

Try awk:

$ echo "http://abc-124-001.portal-ex.xyz-xyz.com:8091/" | awk -F"-" ' { print $2 } '
124
stack0114106
  • 8,534
  • 3
  • 13
  • 38