0

I'm new to bash scripting, and I would like to cut a string given a specific pattern. For example with the string /user/patch.403.json I would like to get and store in a variable /user only. How can I do this with one line of code?

4
  • This can be done in many different ways. It would be good if you indicate what is your final goal or, at least, what is the idea underlying: the first block? everything but the last two? ... Commented Jan 28, 2015 at 15:53
  • Another example with this pattern /user/cards/1212/make_default/put.200.json, I would like to get /user/cards/1212/make_default. My goal is to only get the beginning of the string eliminating /put.200.json with basic commands like cut or sed. Commented Jan 28, 2015 at 15:57
  • 1
    Is this about directories or text? If you got a relative path, say foo.txt, would you want the empty string or .? This looks to me like a case for dirname. Commented Jan 28, 2015 at 15:59
  • this is a text contained in a variable Commented Jan 28, 2015 at 16:05

1 Answer 1

2

As @Wintermute says, you can use the dirname command:

> dirname /user/patch.403.json
/user
> dirname /user/cards/1212/make_default/put.200.json
/user/cards/1212/make_default
> dirname put.200.json
.
Sign up to request clarification or add additional context in comments.

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.