سؤال

If I have the following code:

twitter="https://twitter.com/username"

How do I use something like sed or awk to remove this:

https://twitter.com/

So I'm left with:

username
هل كانت مفيدة؟

المحلول 2

The task can be accomplished by basename:

$ basename https://twitter.com/username
username

نصائح أخرى

Just use bash variable expansion:

$ twitter="https://twitter.com/username"
$ echo "${twitter##*/}"
username

## removes the longest match from the beginning of the string.

You don't need an external program, you can just use normal parameter substitution using the # to remove the prefix:

$ twitter="https://twitter.com/username"
$ echo ${twitter#https://twitter.com/}
username

Using BASH regex capability:

s="https://twitter.com/username"
[[ "$s" =~ /([^/]+)$ ]] && echo "${BASH_REMATCH[1]}"
username

For such a simple task regex might not be always needed but power of regex can be considered for more complex job like finding last but one string between slashes etc.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top