2

I have a really easy problem that I am not able to solve quickly. I must extract a substring from something like this:

a="www.himom.com/byebye"

I want to retrieve this:

"/byebye"

Actually, I am able to retrieve

"www.himom.com" with

echo ${a%/*}

Any suggestions?

6 Answers 6

3

Try this: echo /${a##*/} It deletes the longest substring ending at '/'.

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

Comments

3

Try this:

 a="www.himom.com/byebye"; echo "/${a##*/}"

2 Comments

You will be missing the '/' character that he wants to get.
@izomorphius: Maybe the poster edited the answer, but what I see worked on my machine.
1

To leave the slash in the string

shopt -s extglob
echo "${a##*([^/])}"

Comments

0
$ a="www.himom.com/byebye" | echo ${a##*m}
/byebye

Comments

0

I am a lazy guy.. Solution was:

${a##*/}

1 Comment

Sorry for the multiple edits/rollbacks. I tried to get the code to show correctly, but what I tried didn't work.
-1

You can extract your desired substring with a regex:

a="www.himom.com/byebye"
echo `expr match "$a" '.*\(\/.*\)'`

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.