I have a json file with thousands of JSON objects such as below. I want to compare st1 against st2.

For every object in the JSON file, if st1 is higher than st2, copy every object that matches into another file.

    "in": 4,
    "li": [{
        "fa": 28806,
        "fs": 00002,
        "fb": 21894
    "ome": {
         "ms": 00000000000"
    "st2": 1508584163,
    "ei": {
        "ev": 0
    "rn": 4,
    "st1": 1508584147

I have tried using jq or is it easier doing it with awk even though it's a JSON file?

  • 1
    Your sample shown is not syntactically correct JSON; the comma at the end of line seven implies another element before the closing brace on line eight.
    – DopeGhoti
    Nov 8 '17 at 18:04
  • 2
    what exactly should be copied? elaborate your question Nov 8 '17 at 18:13

Definitely use jq to parse JSON data:

$ cat file.json
{ "st1":0, "st2":10 }
{ "st1":0, "st2":0 }
{ "st1":10, "st2":0 }

$ jq -c 'select(.st1 > .st2)' file.json

This can be redirected into another file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.