1

I have a file called result which looks like this:

{"id":10722,"type":"BRANCH","value":"refs/heads/master","branch":{"id":"refs/heads/master","displayId":"master","latestChangeset":"d53ae5dbaa5e4b2f7b007e94ee91ae2de7e600b6","isDefault":true}}

Using bash, How can I put the substring after "id:" (in this example 10722) inside a parameter? (e.g param=10722)

I need to keep in mind that this file is changing all the time so counting characters is not something I would want to use.

3
  • 2
    Looks like JSON data, better to use jq Commented Nov 22, 2017 at 13:53
  • agree...problem is I can't rely on the fact that jq will be installed because it is a remote server that I have no control over it. Commented Nov 22, 2017 at 13:54
  • You can try, but it leads to ... Commented Nov 22, 2017 at 14:07

1 Answer 1

1

This looks like JSON data. Better to use jq like this:

jq '.id' file.json
10722

If jq is unavailable then use gnu grep:

grep -oP '"id":\K\d+' file.json
10722
Sign up to request clarification or add additional context in comments.

1 Comment

\K directive resets all matched information and \d+ matches 1 or more digits.

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.