如何按编号订购desc或asc特定列

时间:2013-07-03 09:36:11

标签: json sorting awk sed grep

想要通过编号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},

我尝试使用分隔符和coulmn行,但似乎有些错误:

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},

2 个答案:

答案 0 :(得分: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}

答案 1 :(得分: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
  

[{ “状态”:75.75, “姓名”: “DB1”, “usedsize”: “360321”, “MAXSIZE”: “475680”, “大小”: “360612”, “表”: “DAT” },{ “地位”:18.08, “名”: “DB1”, “usedsize”: “23694”, “最大范围”: “131072”, “大小”: “23800”, “表”: “CUNBDAT”}, { “状态”:0.03, “姓名”: “DB1”, “usedsize”: “21”, “MAXSIZE”: “65536”, “大小”: “1000”, “表”: “TEMP”}]