0

想要按数字 desc 或 asc 对列“usedsize”进行排序。

输入:

{"name":"db1","tablespace":"TEMP","size":"1000","maxsize":"65536","usedsize":"21","status":0.03},
{"name":"db1","tablespace":"DAT","size":"360612","maxsize":"475680","usedsize":"360321","status":75.75},
{"name":"db1","tablespace":"CUNBDAT","size":"23800","maxsize":"131072","usedsize":"23694","status":18.08},

我尝试使用分隔符和列行,但似乎有问题:

sort -t: -k10

预期视图:

{"name":"db1","tablespace":"DAT","size":"360612","maxsize":"475680","usedsize":"360321","status":75.75},
{"name":"db1","tablespace":"CUNBDAT","size":"23800","maxsize":"131072","usedsize":"23694","status":18.08},
{"name":"db1","tablespace":"TEMP","size":"1000","maxsize":"65536","usedsize":"21","status":0.03},
4

2 回答 2

1

它不是第 10 列而是第 6 列:

$ sort -t: -k6 file
{"name":"db1","tablespace":"TEMP","size":"1000","maxsize":"65536","usedsize":"21","status":0.03},
{"name":"db1","tablespace":"CUNBDAT","size":"23800","maxsize":"131072","usedsize":"23694","status":18.08},
{"name":"db1","tablespace":"DAT","size":"360612","maxsize":"475680","usedsize":"360321","status":75.75}
于 2013-07-03T09:40:40.747 回答
1

你可以说

sort -t, -k5,5r filename

如果您稍微调整一下输入以使其成为有效的 JSON,您可以像这样使用 perl:

perl -MJSON -00 -le '
    $data = decode_json(<>);
    print encode_json([sort {$b->{usedsize} <=> $a->{usedsize}} @$data]);
' <<END
[
{"name":"db1","tablespace":"TEMP","size":"1000","maxsize":"65536","usedsize":"21","status":0.03},
{"name":"db1","tablespace":"DAT","size":"360612","maxsize":"475680","usedsize":"360321","status":75.75},
{"name":"db1","tablespace":"CUNBDAT","size":"23800","maxsize":"131072","usedsize":"23694","status":18.08}
]
END

[{"status":75.75,"name":"db1","usedsize":"360321","maxsize":"475680","size":"360612","tablespace":"DAT"},{ "status":18.08,"name":"db1","usedsize":"23694","maxsize":"131072","size":"23800","tablespace":"CUNBDAT"},{"status ":0.03,"name":"db1","usedsize":"21","maxsize":"65536","size":"1000","tablespace":"TEMP"}]

于 2013-07-03T12:03:59.627 回答