Puneet
Puneet

Reputation: 215

How to cut a string after last occurrence of a substring in shell?

For a string like this,

"By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service."

I want to cut the string from last occurrence of 'our' to get the result: " Terms of Service."

Upvotes: 1

Views: 2803

Answers (1)

anubhava
anubhava

Reputation: 785058

Using bash you can do this:

s="By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service."
echo "${s##*our }"

Terms of Service.

If you cannot use bash for some reason then this awk should work:

awk -F 'our ' '{print $NF}' <<< "$s"

Terms of Service.

Upvotes: 2

Related Questions