Reputation: 91
I've checked a few articles and found sed
with regex. I mapped the tips to my problem, but without success.
This is not working for me:
echo "uri=https://myserver.domain.de:1234" | sed 's|//\(.+\):|\1|'
I expected
myserver.domain.de
But got the whole string
uri=https://myserver.domain.de:1234
Upvotes: 0
Views: 52
Reputation: 9117
There's no need for sed here. Here's a way to do it using a portable POSIX feature called parameter expansion:
full="uri=https://myserver.domain.de:1234"
withoutport="${full%:[[:digit:]]*}" # Strip the trailing port number (":1234")
desired="${withoutport#uri=https://}" # Strip the undesired prefix
printf '%s\n' "$desired"
You can read more about it here in Open Group Standard Vol. 3: Shell and Utilities, Issue 7: 2.6.2 Parameter Expansion at The Open Group Publications Server.
If you insist on using sed, however, then here is pretty readable solution:
sed -e 's,^uri=https://,,' -e 's,:[0-9]\+$,,'
Upvotes: 0
Reputation: 91
I found this solution now:
echo "uri=https://myserver.domain.de:1234" | sed -r 's|(.+//)([^:]+)(:.+)|\2|'
answer
myserver.domin.de
Upvotes: 0
Reputation: 626689
You need to match the part before the match and after it, and replace +
with *
(or escape the +
, which will make it work in GNU sed
with the BRE POSIX pattern):
echo "uri=https://myserver.domain.de:1234" | sed 's|.*//\(.*\):.*|\1|'
Result: myserver.domain.de
.
See an online demo.
Here is an alternative pattern:
sed 's|.*//\([^:]*\).*|\1|'
where .*
inside the capturing group is replaced with [^:]*
(any 0+ chars other than :
, see below).
Details
.*
- any 0+ chars, as many as possible, up to the last occurrence of subsequent subpatterns//
- a //
substring\(.*\)
- Group 1: any 0+ chars as many as possible (or, to constrain the engine a bit, you may use [^:]*
here instead of .*
(to match any 0+ chars other than :
):
- a colon.*
- the rest of the lineThe \1
backreference will keep the captured value only.
Upvotes: 1