0

Suppose I have the following input.log file:

{"foo": "1", "foo2": "2"}
{"foo": "3", "foo2": "4"}
{"foo": "5", "foo2": "6"}
{"foo": "7", "foo2": "8"}

I want to parse this using jq and sort the result based on the value of some common key, lets say the "foo" key.

How could I accomplish that?

1
  • 1
    Use --slurp and sort_by: jq -sc 'sort_by(.foo)[]' input.log Demo Commented May 27, 2022 at 23:02

1 Answer 1

1

To sort, you need an array, which you can obtain using --slurp/-s.

jq -sc 'sort_by( .foo )[]' input.log

Demo on jqplay

Sign up to request clarification or add additional context in comments.

Comments

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.