1

Say I have a string like

s1="sxfn://xfn.oxbr.ac.uk:8843/xfn/mech2?XFN=/castor/
    xf.oxbr.ac.uk/prod/oxbr.ac.uk/disk/xf20.m.ac.uk/prod/v1.8/pienug_ib-2/reco_c21_dr3809_r35057.dst"

or

s2="sxfn://xfn.gla.ac.uk:8841/xfn/mech2?XFN=/castor/
    xf.gla.ac.uk/space/disk1/prod/v1.8/pienug_ib-2/reco_c21_dr3809_r35057.dst"

and I want in my script to extract the last part starting from prod/ i.e. "prod/v1.8/pienug_ib-2/reco_c21_dr3809_r35057.dst". Note that $s1 contains two occurrences of "prod/".

What is the most elegant way to do this in bash?

1
  • Is it possible for these strings to not include prod/? If so, then -- what is the desired behavior in that case? Commented Aug 6, 2016 at 22:44

3 Answers 3

1

Using BASH string manipulations you can do:

echo "prod/${s1##*prod/}"
prod/v1.8/pienug_ib-2/reco_c21_dr3809_r35057.dst

echo "prod/${s2##*prod/}"
prod/v1.8/pienug_ib-2/reco_c21_dr3809_r35057.dst
Sign up to request clarification or add additional context in comments.

Comments

0

With awk (which is a little overpowered for this, but it may be helpful if you have a file full of these strings you need to parse:

echo "sxfn://xfn.gla.ac.uk:8841/xfn/mech2?XFN=/castor/xf.gla.ac.uk/space/disk1/prod/v1.8/pienug_ib-2/reco_c21_dr3809_r35057.dst" | awk -F"\/prod" '{print "/prod"$NF}'

That's splitting the string by '/prod' then printing out the '/prod' delimiter and the last token in the string ($NF)

Comments

0

sed can do it nicely:

s1="sxfn://xfn.oxbr.ac.uk:8843/xfn/mech2?XFN=/castor/xf.oxbr.ac.uk/prod/oxbr.ac.uk/disk/xf20.m.ac.uk/prod/v1.8/pienug_ib-2/reco_c21_dr3809_r35057.dst"
echo "$s1" | sed 's/.*\/prod/\/prod/'

this relies on the earger matching of the .* part up front.

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.