Home > Software design >  jq transform to array and sort
jq transform to array and sort

Time:05-29

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? Thank you.

CodePudding user response:

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

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

Demo on jqplay

  • Related