k1308517 k1308517 - 1 year ago 79
Linux Question

Simple bash get part of a string

I have this text and more stored in a



How can I find id, and then get just the number?

There will be a lot of other text in the string.

Whole string is:
After making says:


I cannot use any JSON parsers, must be with string manipulation!

Answer Source

If you want to parse a JSON, use jq!

$ jq '.id' file

And if you don't want to get the quotes, use -r:

$ jq -r '.id' file

From the manual:

--raw-output / -r:

With this option, if the filter’s result is a string then it will be written directly to standard output rather than being formatted as a JSON string with quotes. This can be useful for making jq filters talk to non-JSON-based systems.

Note I had to tweak your JSON and write "projectId":"xxxxx" so that it is a valid one.