0

The following is the format of data that I need to parse in bash. Assuming that a bash variable holds this data, I need to be able to extract the value in xyz. Further, I need to be able to also extract aa and bb individually from with xyz.

"params": {
    "children": [
           {
                "abc": {
                    "pp": "1234567890",
                    "qq": "a.b.c"
                },
                "xyz": {
                    "aa": "0987654321",
                    "bb": "c.b.a"
                },
                "def": "p.q.r"
            }
        ],
        "def": "e.f.g.h"
    }

Any help on this is appreciated!

MLSC
  • 5,872
  • 8
  • 55
  • 89
Maddy
  • 1,319
  • 3
  • 22
  • 37

1 Answers1

1

The shell is not a good language for writing general parsers. That said, if your input format is more or less exactly as shown, with maximally one "name": "value" pair per line, try this:

$ eval $(sed -n 's/"\([^"]*\)"[ :]*"\([^"]*\)".*/\1=\2/p' inputfile)
$ echo $aa
0987654321
$ echo $bb
c.b.a
Jens
  • 69,818
  • 15
  • 125
  • 179
  • What if I have 'aa' and 'bb' under both 'abc' and 'xyz'? How would I retrieve a specific line? – Maddy Dec 04 '13 at 08:30
  • Then I suggest using a tool matching the problem, i.e. a proper parser, which the shell isn't. – Jens Dec 04 '13 at 08:40