1
ns/APAC_BankTransfers_Publish/CMB/services/svcPublishBankTransfers/flow.xml

In the above line and similar lines like this I want to extract whatever is present in between services and flow.xml and save it to a variable DIST.

The output should be svcPublishBankTransfers.

3 Answers 3

5

Using parameter expansion mechanisms available in POSIX sh:

s=ns/APAC_BankTransfers_Publish/CMB/services/svcPublishBankTransfers/flow.xml
s=${s%/flow.xml}    # remove "/flow.xml"
s=${s##*/services/} # remove everything before "services"
echo "$s"

This has the advantages of being purely in-process (so faster than approaches that require piping through an external tool), and compatible with all POSIX shells (ash, dash, ksh, etc).

References:

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

Comments

1

Using BASH regex:

s='ns/APAC_BankTransfers_Publish/CMB/services/svcPublishBankTransfers/flow.xml'
[[ "$s" =~ /([^/]+)/[^/]*$ ]] && echo "${BASH_REMATCH[1]}"
svcPublishBankTransfers

OR else:

[[ "$s" =~ /services/([^/]+)/flow\.xml ]] && echo "${BASH_REMATCH[1]}"
svcPublishBankTransfers

Comments

0
bash$ echo "ns/APAC_BankTransfers_Publish/CMB/services/svcPublishBankTransfers/flow.xml" | cut -d '/' -f 5
svcPublishBankTransfers
bash$

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.