0

For instance I have a JSON file like this:

{
    "mac": "00:11:22:33:44:55",
    "name: "Test123",
    "ssid": "29321",
    "password": "txt",
    "data": {
        "test:": "no",
        "dev": "yes",
        "prod": false
    },
    "signals": [12, 34, 65, 93, 21],
}

I'd like to grep a particular JSON key (including subkeys) from data above, but when I cat that file or read a JSON file from somewhere else in the output stream, ie wget or curl.

So far I have tried only this:

cat json.txt | grep -o -E '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}'

This returns the "mac" key value, grep's it by a pattern, strips all the quotes in the output and it really works flawlessly!

I honestly haven't tried anything regardings JSON keys, since any regular-expression system is not my stronger point and I hope you'll all help me about this.

NOTE: I'm very limited on the system where I'll be running this, so the -P option is not available.


EDIT: Actually, we do have root access to devices. They're running on MIPS platform and we have no compilers on the system installed. All the apps we can install must be already precompiled, so I doubt I'll find jq or jshon for these devices.

If there's another tool, that'd be helpful, probably.


EDIT 2: Issuing command: akw --version, we get this response:

BusyBox v1.19.‎4 (2017-03-27 19:07:06 EEST) multi-call binary.
8
  • 5
    Use a json parser like jq. Commented Apr 18, 2017 at 20:29
  • I would, but I'm very limited on my system where I'd be running this - can't install software, I have a limited version of grep, etc. Commented Apr 18, 2017 at 20:31
  • It's a system which comes on Wi-Fi broadband equipment from this vendor: ubnt.com - it's based on Linux, runs an old version of PHP 4 (or something), we don't have the root access to it; just a tmp directory where we can store our files, and/or execute available commands. Commented Apr 18, 2017 at 20:32
  • 1
    You don't need a compiler on the system to compile software for it. People professionally building software for these systems don't run compilers on little embedded devices either. Look up cross compilation. Commented Apr 18, 2017 at 21:36
  • 1
    Personally, I'm a big fan of Buildroot as tooling for building system images for arbitrary targets -- though it doesn't need to be a full system image; you could just as easily build a single static executable that way. Commented Apr 18, 2017 at 21:37

1 Answer 1

1

Well using jq would be the ideal option but you could use sed to close

sed -n '/ *"keyname": */{ s///; s/ *, *$//; s/"//g; p; }'
Sign up to request clarification or add additional context in comments.

1 Comment

With very specific constraints (line-based layout, retrieving scalars only, no awareness of hierarchy, …) this may work, but I suggest stating these constraints in your answer. Perhaps also show how to specify the key name as a variable.

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.