Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

6
  • Note that if the value of e.g. Name contains the string Value, this may return unexpected data. Commented Jan 22, 2020 at 16:59
  • @Kusalananda Indeed. Using something like jq to parse the data rather than pattern match is clearly the better solution (like using sh rather than csh). The answer given is to the question as asked. I pointed out in the comments that jq would be a suitable tool, and the OP clarified that he didn't want to install any extra packages. Commented Jan 22, 2020 at 17:04
  • @Kusalananda raises a valid concern. Within the constraint of not using jq, I'd personally be inclined to match the chosen line with /^ *"Value": "/ rather than just /Value/, in order to (I think) eliminate any possibility of a match with other lines of the input. Commented Apr 20 at 11:49
  • @Neilski It would cut down on false matches at the expense of missing correct matches. For example the whitespace can be tab characters. The question is really "what is good enough?" for a "one liner". JSON data does not be laid out one value per line, but the output from the aws command does. "Value" doesn't have any characters which have special meaning in regular expressions. We could implement a proper JSON parser in awk, and technically do it in a single (very long) line. This is the tradeoff of engineering vs mathematics. Commented Apr 20 at 14:04
  • @icarus Yup, my comment was focussed purely on handling the presented output from aws, but indeed for more general JSON handling you'd want to tweak it and/or accept a limited alignment with the JSON standard. However, as a bare minimum I'd suggest inclusion of the double quotation marks around Value and the colon which must come between it and the JSON 'value', no? (I'm unable to understand what you mean by '"Value" doesn't have any characters which...'; surely searching for "Value" is better than searching for Value?) Commented Apr 21 at 0:02