cat ... | sort-by your command ...
$ cat toto.jsons
{"a": 1, "b": 2}
{"b": 1, "a": 3}
{"b": 3, "a": 5}
{"b": 0}
$ cat toto.jsons | sort-by 'jq .b'
{"b": 0}
{"b": 1, "a": 3}
{"a": 1, "b": 2}
{"b": 3, "a": 5}
#! /bin/bash -e | |
__sort_by() { | |
local sep="|" # arbitrary | |
local cmd="$*" | |
local tmpfile=$(mktemp) | |
local res= | |
while read line; do | |
res=$(echo "$line" | $cmd) | |
echo "$res$sep$line" >> $tmpfile | |
done | |
sort $tmpfile | cut "-d$sep" -f2 | |
} | |
__sort_by $* |