开发者社区> 问答> 正文

利用shell将json数据解析后排序问题

最近在写一个模仿dropbox_uploader的百度网盘的shell脚本。通过调用pcs的api返回一个目录下的所有文件。返回的json数据如下:

{"list":[{"fs_id":1878243434,"path":"\/apps\/zjhui\/test","ctime":1392866001,"mtime":1392866001,"md5":""
利用JSON.sh解析后如下:

["list",0,"fs_id"]  1878243434
["list",0,"path"]   "\/apps\/zjhui\/test"
["list",0,"ctime"]  1392866001
["list",0,"mtime"]  1392866001
["list",0,"md5"]    ""
["list",0,"size"]   0
["list",0,"isdir"]  1
["list",1,"fs_id"]  3901347206
["list",1,"path"]   "\/apps\/zjhui\/upload"
["list",1,"ctime"]  1392860719
["list",1,"mtime"]  1392860719
["list",1,"md5"]    ""
["list",1,"size"]   0
["list",1,"isdir"]  1
["list",2,"fs_id"]  3190602252
["list",2,"path"]   "\/apps\/zjhui\/aaaaaaaa"
["list",2,"ctime"]  1392885465
["list",2,"mtime"]  1392885465
["list",2,"md5"]    "783b7e40cdfb4a1344d15b1f7081af66"
["list",2,"size"]   35121
["list",2,"isdir"]  0
["list",3,"fs_id"]  3734744594
["list",3,"path"]   "\/apps\/zjhui\/baidu-pcs.txt"
["list",3,"ctime"]  1390898949
["list",3,"mtime"]  1390898949
["list",3,"md5"]    "5ccce0498cf29996399750789f415bb8"
["list",3,"size"]   450
["list",3,"isdir"]  0
["list",4,"fs_id"]  4176732013
["list",4,"path"]   "\/apps\/zjhui\/bash"
["list",4,"ctime"]  1389845838
["list",4,"mtime"]  1389845838
["list",4,"md5"]    "286dc423e71186936c39e3507d2d0cf1"
["list",4,"size"]   2076
["list",4,"isdir"]  0
["list",5,"fs_id"]  1122970857
["list",5,"path"]   "\/apps\/zjhui\/LICENSE"
["list",5,"ctime"]  1391567032
["list",5,"mtime"]  1391567032
["list",5,"md5"]    "783b7e40cdfb4a1344d15b1f7081af66"
["list",5,"size"]   35121
["list",5,"isdir"]  0
["list",6,"fs_id"]  3086666598
["list",6,"path"]   "\/apps\/zjhui\/LICENSE-0205"
["list",6,"ctime"]  1391569592
["list",6,"mtime"]  1391569592
["list",6,"md5"]    "783b7e40cdfb4a1344d15b1f7081af66"
["list",6,"size"]   35121
["list",6,"isdir"]  0
["list",7,"fs_id"]  4120839796
["list",7,"path"]   "\/apps\/zjhui\/LICENSE01"
["list",7,"ctime"]  1391569447
["list",7,"mtime"]  1391569447
["list",7,"md5"]    "783b7e40cdfb4a1344d15b1f7081af66"
["list",7,"size"]   35121
["list",7,"isdir"]  0
["list",8,"fs_id"]  2922247596
["list",8,"path"]   "\/apps\/zjhui\/Pcs.sh"
["list",8,"ctime"]  1389857060
["list",8,"mtime"]  1389857060
["list",8,"md5"]    "aaeafb64fc4efcff24de3b4e92dc302f"
["list",8,"size"]   3355
["list",8,"isdir"]  0
["list",9,"fs_id"]  1874426057
["list",9,"path"]   "\/apps\/zjhui\/Pcs_Uploader.sh"
["list",9,"ctime"]  1389773672
["list",9,"mtime"]  1389773672
["list",9,"md5"]    "14d6e4f7b9daf7761a7f2aa83efaa64d"
["list",9,"size"]   2653
["list",9,"isdir"]  0
["list",10,"fs_id"] 2295037712
["list",10,"path"]  "\/apps\/zjhui\/test1"
["list",10,"ctime"] 1392869356
["list",10,"mtime"] 1392869356
["list",10,"md5"]   "5fceae3ba3777db47ed1cc6a9a96cf27"
["list",10,"size"]  728
["list",10,"isdir"] 0
["request_id"]  1194090611  
我想通过解析这json数据,然后进行组装,最后得到类似于./dropbox.sh list的效果:

全选复制放进笔记 [F] 35147   aaaaaaa
 [F] 9524791 Agile_Web_Development_with_Rails_4th_for_Rails_3.2.pdf
 [D] 0       Camera Uploads
 [D] 0       Photos
 [D] 0       Public

展开
收起
a123456678 2016-06-17 16:56:40 2869 0
1 条回答
写回答
取消 提交回答
  • 码农|Coder| Pythonista
    $ cat data | jshon -e list -a -e isdir -u -p -e size -u -p -e path -u | sed 'N;N;s/^1/[D]/;s/^0/[F]/;s/\n/\t/g'
    [D]     0       /apps/zjhui/test
    [D]     0       /apps/zjhui/upload
    [F]     35121   /apps/zjhui/aaaaaaaa
    [F]     450     /apps/zjhui/baidu-pcs.txt
    [F]     2076    /apps/zjhui/bash
    [F]     35121   /apps/zjhui/LICENSE
    [F]     35121   /apps/zjhui/LICENSE-0205
    [F]     35121   /apps/zjhui/LICENSE01
    [F]     3355    /apps/zjhui/Pcs.sh
    [F]     2653    /apps/zjhui/Pcs_Uploader.sh
    [F]     728     /apps/zjhui/test1

    jshon 请自己安装。要排序自己按需求接 sort 命令。

    2019-07-17 19:43:08
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
ECS块存储产品全面解析 立即下载
神龙云服务器产品及技术深度解析 立即下载
弹性创造价值:基于ECS的最佳性价比实践解析 立即下载

相关镜像